EVT_CHAR and EVT_TEXT

63q2o4i02 at sneakemail.com 63q2o4i02 at sneakemail.com
Fri Sep 8 00:58:05 PDT 2006


Hi, I'm having some confusion with EVT_CHAR and EVT_TEXT.  Perhaps
someone could enlighten me. (wxPython 2.6.3.2, WinXP SP2)

I have the following:

class Form1(wx.Panel):
	def __init__(self, parent, id):
		self.editname = wx.TextCtrl(...ID=20...)
	# Pick one from #1-8
	#1	self.Bind(wx.EVT_TEXT, self.EvtText); # endless loop
	#2	self.Bind(wx.EVT_TEXT, self.EvtText, self.editname); # OK
	#3	self.editname.Bind(wx.EVT_TEXT, self.EvtText); # OK
	#4	self.editname.Bind(wx.EVT_TEXT, self.EvtText, self.editname); # OK

	#5	self.Bind(wx.EVT_CHAR, self.EvtChar); # Nothing
	#6	self.Bind(wx.EVT_CHAR, self.EvtChar, self.editname); # Nothing
	#7	self.editname.Bind(wx.EVT_CHAR, self.EvtChar); # OK
	#8	self.editname.Bind(wx.EVT_CHAR, self.EvtChar, self.editname); OK

	def EvtText(self, event):
		self.logger.AppendText('Text: %s\n' % event.GetString())

	def EvtChar(self, event):
		self.logger.AppendText('Char: %d\n' % event.GetKeyCode())

I'm trying to get both EvtText() and EvtChar() to run whenever a key is
pressed in the TextCtrl.  Can someone please confirm for me my
understanding of these different binding variations (after reading the
binding wiki here
http://wiki.wxpython.org/index.cgi/self.Bind_vs._self.button.Bind):

1.  When EvtText is called, it prints text in another (not editname)
text box, which generates an unhandled event.  Since no object or ID is
specified in #1, it catches it and starts the loop again.
2.  Same as above, but only get called when event comes from the
editname object, so it works ok.
3.  Event is captured in editname, and since there is only one event
generator at this level (itself), then no loop is created.
4.  Redundant with #3.
5.  Since EVT_CHAR does not propagate, the Form1 object never sees the
event.
6.  Same as #5.
7.  Event is captured in editname object since it's its own event
generator, and because of this, no funny loop is created.
8.  Redundant with 7.

These work individually as shown.  However, when I try catching both
events, say with #2 and #7, only the wxKeyEvent is processed, unless I
do an event.Skip() in EvtChar(), then EvtText() is called too.  The
only reason I can think of is that the KeyEvent is used to create the
Text event, and if it's been "used up" in the EvtChar() handler without
Skip()ing it, then no text event can be created.

Is this true?

I have some questions:
1.  Can the Bind() function bind any handler function to any event?  In
the examples above, what happens if EvtChar() is defined somewhere not
even in the heirarchy, eg a global function?  Could it, for the sake of
argument, be a function defined in the object that generated the event,
but be called only when the event gets to a parent object?  This is a
silly example, but i'm trying to understand.

2.  What is the relationship between the wxKeyEvent class and its
related events, wx.EVT_CHAR, wx.EVT_KEY_DOWN, and wx.EVT_KEY_UP?  I
don't quite understand the semantics and when you use one and when you
use the other.  Python tells me these are "wx._core.PyEventBinder"
objects rather than anything that looks like an "event" per se.  Do I
care?

3.  Where/when in the Bind() method do I use an ID of any sort, if I
have access to the object itself, as in #2, #4, #6, #8?

With so many ways to bind events (Bind(), Connect(), python "macros"
such as EVT_CHAR(...)), and so many different angles to this event
handling business, it's all very confusing, esp. when trying to map the
c++ documentation onto the python world.

thanks a lot for any input
ms







More information about the wx-users mailing list