I’m mentoring a high school student who’s learning wxPython. Anyway, he’s reading wxPython in Action as his textbook and just finished chapter 3, which is on events. He wanted me to come up with an exercise and I gave him a few things to try, including trying to demo how event.Skip() works to propagate events, although that last one was optional. Anyway, he can’t get that bit to work and I’m a little weak in understanding some of the idiosyncrasies at work here too. Here’s his questions on the topic:
Skip() doesn't propagate certain events. For example, when I bind
wx.EVT_ENTER_WINDOW to both the button and the frame, only the handler
bound to the button fires; when I bind wx.EVT_BUTTON, both handlers
fire.
I am also confused as to how event.GetEventObject() works. When I
called it, a random parent window is returned, not the source window
itself. Why is this?
I checked the latter one out and it looks like when the event comes from the menu, the GetEventObject call returns the frame instead of the menu or menubar object. I’m not particularly surprised by that, but could someone explain it anyway? I assuming the first one has something to do with CommandEvents?
I'm mentoring a high school student who's learning wxPython. Anyway,
he's reading wxPython in Action as his textbook and just finished
chapter 3, which is on events. He wanted me to come up with an exercise
and I gave him a few things to try, including trying to demo how
event.Skip() works to propagate events, although that last one was
optional. Anyway, he can't get that bit to work and I'm a little weak in
understanding some of the idiosyncrasies at work here too. Here's his
questions on the topic:
<questions>
Skip() doesn't propagate certain events. For example, when I bind
wx.EVT_ENTER_WINDOW to both the button and the frame, only the handler
bound to the button fires; when I bind wx.EVT_BUTTON, both handlers
fire.
By default events for which the event class derives from wx.ComamndEvent will propagate up to parent windows if they are not handled or if they are Skipped. Those that do not derive from wx.CommandEvent are sent only to the window in which the event happens and do not propagate. See the "wx.Event vs. wx.CommandEvent" and "Skipping Stones" sections at self.Bind vs. self.button.Bind - wxPyWiki
I am also confused as to how event.GetEventObject() works. When I
called it, a random parent window is returned, not the source window
itself. Why is this?
</questions>
I checked the latter one out and it looks like when the event comes from
the menu, the GetEventObject call returns the frame instead of the menu
or menubar object. I'm not particularly surprised by that, but could
someone explain it anyway?
For some obscure reason lost in the mists of ancient history menu events coming from a menu bar are considered to have originated from the frame rather than from the menu. Internally it is actually code that belongs to the frame that is catching the native message and sending the event, so that probably has something to do with it.