[wxPython-users] Loops and Binds and Unbinds

Saketh Bhamidipati saketh.bhamidipati at gmail.com
Mon Jul 17 17:39:26 PDT 2006


On 7/17/06, Walter Igo <walterigo at yahoo.com> wrote:
>
> I am having troubble restarting the wx.EVT_TEXT after processing the text
> input. I need to trun off the wx.EVT_TEXT processing so the Append Text
> will not keep looping because the text event keeps updating.
> How can I trun back on the wx.EVT_TEXT event after I am done with the
> processing.
>
> here is the code.
>
> import wx
> import re
> class ExpApp(wx.App):
>     def OnInit(self):
>         frame=3DExpFrame("Expednte",(50,60),(450,340))
>         frame.Show()
>         self.SetTopWindow(frame)
>         return True
> class ExpFrame(wx.Frame):
>     def __init__(self,title,pos,size):
>         wx.Frame.__init__(self,None,-1,title,pos,size)
>         panel=3Dwx.Panel(self,-1)
>         self.TextWindow=3Dwx.TextCtrl(panel,size=3D(440,282),style=3D
> wx.TE_MULTILINE|wx.TE_RICH2)
>         #self.TextWindow.Bind(wx.EVT_KEY_DOWN,self.CheckKeyPress)
>         self.TextWindow.Bind(wx.EVT_TEXT,self.CharLoop)
>         #self.CreateStatusBar()
>     def CheckKeyPress(self,event):
>         print 'CheckKeyPress Ran'
>         self.TextWindow.Bind(wx.EVT_TEXT,self.CharLoop)
>         self.TextWindow.Bind(wx.EVT_KEY_DOWN,None)
>     def CharLoop(self,event):
>         pos =3D wx.TextCtrl.GetInsertionPoint(self.TextWindow)
>         text=3Dself.text =3D re.split('[^a-zA-Z0-9]+', event.GetString())=
[-1]
>         #self.text=3Devent.GetKeyCode()
>         if len(text)>1:
>             found=3DFalse
>             self.TextWindow.Bind(wx.EVT_TEXT,None)  #Keeps EVT_TEXT From
> Updating
>             while not found:
>                 for num, colVal in enumerate(dynamic_choices):
>                     choice=3Dre.match(text,dynamic_choices[num])
>
>                     if choice:
>                         CurentChoice=3Ddynamic_choices[num]
>                         self.TextWindow.AppendText(colVal[pos:])
>                         self.TextWindow.SetStyle(pos,len(colVal),
> wx.TextAttr("RED", "YELLOW"))
>                         found=3DTrue
>                         return
>                         while enumerate:
>                             print num
>                             print "eles statement"
>                             self.TextWindow.Bind(wx.EVT_TEXT,self.CharLoop
> )
>
>
>
I wouldn't re-bind the wx.EVT_TEXT to None as you did in CharLoop. I would
use a flag (like self.processFlag =3D True), and check whether it is true or
false at the beginning of CharLoop. If it is true, then continue to process;
if false, then return. Using a flag is preferable to changing the event
bindings, and it's easier to check the status of the flag for debugging.

Also, it's a good idea to name bound functions with the prefix "On" or "on".
Just for clarity, I would rename CharLoop to "OnText" or "onText", and maybe
shift the logic to a separate method. You don't have to do this, but I think
it's a good habit.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200607=
17/987b48da/attachment.htm


More information about the wxpython-users mailing list