[wxPython-users] Re: TextCtrl eating all events when empty in MS Windows

Robin Dunn robin at alldunn.com
Fri Mar 2 12:39:28 PST 2007


Sid Wiesner wrote:
> Robin Dunn wrote:
>> Please create a small runnable sample that shows the problem so we can 
>> see exactly what you are trying to do.
>>
>> http://wiki.wxpython.org/index.cgi/MakingSampleApps
>>
> 
> Okay, here is an example. If you run it, it will just show a text 
> control. On Mac OS X, if the control is empty and you press backspace or 
> any other keys, events are generated. On Windows, if it is empty and 
> keys are pressed (other than normal ASCII keys), no events are 
> generated. Any ideas?

Actually, the bug here is on Mac, not Windows.  The EVT_TEXT event is 
sent when the value of the textctrl changes, and if the control is empty 
and you press backspace, there is no change in value so there should be 
no event.

> 
> #!/usr/bin/env python
> 
> import wx
> 
> class Frame(wx.Frame):
> 
>     def __init__(self, parent, id):
>         wx.Frame.__init__(self, parent, id, 'Broken on windows')
>         panel = wx.Panel(self)
>         textCtrl = wx.TextCtrl(panel, wx.NewId(), "")
> 
>         self.Bind(wx.EVT_CHAR, self.onKeyPress)

Also, since EVT_CHAR is not a command event you won't get any of them by 
binding to the frame.  Try textCtrl.Bind(...) instead.


>         self.Bind(wx.EVT_TEXT, self.onTextCtrlChange)
> 
>     def onKeyPress(self, event):
>         print "key press:", event.GetKeyCode()

Finally, if you want the EVT_CHAR to still be processed by the widget 
(otherwise it won't be able to add the characters to its value) then 
you'll need to call event.Skip() here.

http://wiki.wxpython.org/index.cgi/self.Bind_vs._self.button.Bind


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list