EVT_CHAR and EVT_TEXT

63q2o4i02 at sneakemail.com 63q2o4i02 at sneakemail.com
Fri Sep 8 09:28:28 PDT 2006



Robin Dunn wrote:
> 63q2o4i02 at sneakemail.com wrote:
> > Hi, I'm having some confusion with EVT_CHAR and EVT_TEXT.  Perhaps
> > someone could enlighten me. (wxPython 2.6.3.2, WinXP SP2)
> >
> >
> > 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.
>
> Correct.
>
> > 2.  Same as above, but only get called when event comes from the
> > editname object, so it works ok.
>
> Correct.
>
> > 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.
>
> Correct.
>
> > 5.  Since EVT_CHAR does not propagate, the Form1 object never sees the
> > event.
> > 6.  Same as #5.
>
> Correct
>
> > 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.
>
> Correct.
>
> >
> > 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?
>
> Basically.  When you intercept the EVT_CHAR and don't Skip() it then the
> native control does not get the EVT_CHAR event, so it does not do
> whatever the native control does in response (such as insert the char
> into the control, send the EVT_TEXT, etc.)
>
>
> >
> > 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.
>
> Any callable object can be used as an event handler, even, for example,
> instances of a class that has a __call__ method.  The only requirement
> is that it expect just the single event parameter.
>
>
> >
> > 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?
>
> No need to care.  The event binders are just a mechanism used to
> associate a type of event (not the event class, but a type ID) to a
> handler for a particular widget, and do it in a way that makes sense for
> the Python programmer and also fulfills the needs of the C++ event tables.
>
>
> >
> > 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?
>
> If you pass the source parameter then Bind uses source.GetId() to get
> the ID to pass on to the C++ code.
>
>
> >
> > 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.
>
> Calling the event binder objects as functions is a hold over from the
> old way of doing things from before Bind() was added, and is only
> retained for preserving compatibility with old code.  Connect() is used
> internally inside of both Bind and when using the event binders as
> functions.  You should never need to use Connect from Python code.
>
>


Great, thanks a lot for the clarification.  I learned a lot between
beginning my original post and finishing it, since I was trying to be
as concise as possible.  Turns out I pretty much managed to figure it
out, but I wanted to make sure I understood what's going on.

So if I wanted to use IDs with Bind() can I, or should I?  I've seen
arguments that you'd [usually?] only want to use IDs when you have
several controls, possibly from different dialogs and stuff, mapping to
the same function, like fileopen or something.  I like the cleanliness
of NOT using IDs.

ms







More information about the wx-users mailing list