Discussion:
Binding all children to a common event and event handler
Nathan McCorkle
2014-09-17 23:51:05 UTC
Permalink
I was trying to post this as a reply to an existing thread, but Google
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown, child)
. . .which didn't work, probably because (again) the events don't
propagate.
I made a function which seems to work for me (I was trying to enable the
Escape key to fire a Close event no matter where on the Dialog it was
pressed):

def __init__(self, parent):
#do init
#bind all this and all the children to the KEY_DOWN event
self.bindAllChildren(self, wx.EVT_KEY_DOWN, self.lookForEsc)

def lookForEsc(self, event):
if event.GetKeyCode()== wx.WXK_ESCAPE:
self.EndModal(wx.ID_CANCEL)
event.Skip()

def bindAllChildren(self, obj, event, handler):
if isinstance(obj, (list, wx.WindowList)):
for sub_obj in obj:
self.bindAllChildren(sub_obj, event, handler)
if hasattr(obj, 'Bind'):
obj.Bind(event, handler)
if hasattr(obj, 'GetChildren'):
obj = obj.GetChildren()
if obj:
self.bindAllChildren(obj, event, handler)

I'll post an update if I find this breaking things.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nathan McCorkle
2014-09-18 00:30:58 UTC
Permalink
Post by Nathan McCorkle
I made a function which seems to work for me (I was trying to enable the
Escape key to fire a Close event no matter where on the Dialog it was
I'll post an update if I find this breaking things.
While it didn't seem to break anything, I found that Dialog already handles
global escape keypresses... BUT ONLY IF you have at least one Button with
either id=wx.ID_OK or id=wx.ID_CANCEL

I didn't notice this default feature at first because I am so used to using
-1 for all my widget IDs.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...