PDA

View Full Version : wxPython - Set up events/handlers with less coding?


dodle
April 19th, 2009, 06:14 AM
Is there a way to set up events/event handlers using a single line for multiple buttons/objects? I already tried adding the buttons to a list, then binding the list.... that doesn't work.

For example if I bind three buttons, I use three lines of code:
button1.Bind(wx.EVT_BUTTON, self.EventHandler)
button2.Bind(wx.EVT_BUTTON, self.EventHandler)
button3.Bind(wx.EVT_BUTTON, self.EventHandler)

or just connecting:
wx.EVT_BUTTON(button1, -1, self.EventHandler)
wx.EVT_BUTTON(button2, -1, self.EventHandler)
wx.EVT_BUTTON(button3, -1, self.EventHandler)

dodle
April 27th, 2009, 06:22 AM
button1 = wx.Button(self, -1, "1")
button2 = wx.Button(self, -1, "2")
button3 = wx.Button(self, -1, "3")

button_group = [button1, button2, button3]

for entry in button_group:
entry.Bind(wx.EVT_BUTTON, self.OnButton)

Sprut1
April 27th, 2009, 06:42 AM
I also think I once connected all button events to 1 handler like this:


wx.Bind(wx.EVT_BUTTON, Handler)

dodle
May 2nd, 2009, 04:01 AM
Thanks, that could be useful.

Sprut1
May 2nd, 2009, 02:26 PM
Thanks, that could be useful.

Ye, here is a discussion I started a long time ago:
http://ubuntuforums.org/showthread.php?t=1000790

Notice in last post, using


event.GetEventObject().GetId()


in handler will grant you the id of which button was pressed. This might be usefull if you can manage all the ID's :)