From: Josiah Carlson
Sent: 25 May 2007 17:48>
You may find the updated event creation and processing mechanism to be
easier to write and understand:
CustomEventClasses - wxPyWiki
Thanks for the timely response. I hadn't stumbled across CustomEventClasses.
For your particular issue, I would create a 'CommandEvent', then bind
your event handler to Top.Alternatively, you could use wx.lib.pubsub and bypass the custom event
hierarchy entirely.
I'm considering pubsub, or dispatcher, or even the whole docview framework since that's what my app is starting to look like.
I ended up skim reading the wxWidgets docs (which I probably should have done first) on event handling,
and binding the child to the Top's event handler by doing:
wx.GetApp().GetTopWindow().Connect(wx.ID_ANY, wx.ID_ANY, EVT_TYPE_FOO, self.OnFoo)
or turned into an external function:
def _bindToSourcelessEvent(typeEvent, handlerBind, evthandlerBind=None):
"""Bind the specified function to the specified event
regardless of source
Parameters:
typeEvent - Type of event to bind
handlerBind - Callable handler to bind to
evthandlerBind - Event handler which will receive event
"""
if evthandlerBind is None: #If no event handler specified
#Use top level window
evthandlerBind = wx.GetApp().GetTopWindow()
evthandlerBind.Connect(wx.ID_ANY, wx.ID_ANY, typeEvent, handlerBind)
#Lets me do something like
class WinFoo(wx.Frame):
def __init__(self, *a, **kw):
wx.Frame.__init__(self, *a, **kw)
_bindToSourcelessEvent(EVT_TYPE_FOO, self.OnEventFoo)
As I understand it this approach relies on the Top window seeing any custom event instance pass through its ProcessEvent, which in practice means that any window which wants to raise an event like the following:
def doSomething(self):
evtTrigger = events.EventFoo(EVT_TYPE_FOO, 0)
self.GetEventHandler().ProcessEvent(evtTrigger)
needs to be a child of the Top (so that "GetEventHandler" is looking up the correct hierarchy). As it stands that is not much of a restriction for me at the moment. It seems to do what I want which is allow one window to raise an event without caring about who's interested in it, and another to register an interest in that event via _bindToSourcelessEvent() without worrying about where it came from.
However, it smells like a hack on my part based on incomplete knowledge, so I'll try to make time to read the links you've kindly provided.
···
______________________________________________________________________
This email is intended only for the use of the individual(s) to whom it is addressed and may be privileged and confidential.
Unauthorised use or disclosure is prohibited.If you receive This e-mail in error, please advise immediately and delete the original message.
This message may have been altered without your or our knowledge and the sender does not accept any liability for any errors or omissions in the message.