Today on StackOverflow I saw someone wondering how to bind two functions / methods to the same event in wxPython. It’s really quite easy. Here’s one example:
import wx ######################################################################## class MyPanel(wx.Panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """Constructor""" wx.Panel.__init__(self, parent) btn = wx.Button(self, label="Press Me") btn.Bind(wx.EVT_BUTTON, self.HandlerOne) btn.Bind(wx.EVT_BUTTON, self.HandlerTwo) #---------------------------------------------------------------------- def HandlerOne(self, event): """""" print "handler one fired!" event.Skip() #---------------------------------------------------------------------- def HandlerTwo(self, event): """""" print "handler two fired!" event.Skip() ######################################################################## class MyFrame(wx.Frame): """.""" #---------------------------------------------------------------------- def __init__(self): """Constructor""" wx.Frame.__init__(self, None, title="Test") panel = MyPanel(self) self.Show() if __name__ == "__main__": app = wx.App(False) frame = MyFrame() app.MainLoop()
As you can see, all you had to do was call the widget’s Bind method twice and pass it the same event, but different handlers. The next key piece is that you have to use event.Skip(). Skip will cause wxPython to look for other handlers that may need to handle the event. Events travel up the hierarchy to the parents until they’re handled or nothing happens. The book, Wxpython in Action” target=”_blank”>wxPython in Action by Robin Dunn explains this concept really well.
event.Skip() it’s the key.
Thank you.