In wxPython code, I've got a set of buttons with similar behavior. I want to use a single handler function for all the buttons, and am using a lambda function to set up an extra argument which will indicate some characteristic of the button. I've done this with inline code, but I'm trying to do it in a loop. In the following example, the output to the console always prints an index of 2 (the index for th last button) regardless of which button is pressed. On the other hand, EventObject.GetLabel() does the right thing in each case.
I also constructed a similar, non-gui test case (not included), so I *think* I'm using lambda properly. Any ideas?
#base code borrowed from "wxPython In Action" listing 3.3
import wx
class MouseEventFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, title="Testing", size=(300, 120))
self.panel = wx.Panel(self)
for i in range(3):
self.button = wx.Button(self.panel, label="Button "+str(i), pos=(100, 15 + 20*i ))
self.Bind(wx.EVT_BUTTON, lambda evt : self.OnSetRank(evt, i), self.button)
self.button.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow)
self.button.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)
def OnSetRank(self, event, index):
print index
print event.EventObject.GetLabel()
def OnEnterWindow(self, event):
self.button.SetLabel("Over")
event.Skip()
def OnLeaveWindow(self, event):
self.button.SetLabel("Not over")
event.Skip()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MouseEventFrame(None, id=-1)
frame.Show()
app.MainLoop()