0

Is there a way I can bind a function with parameter to menu items in 'for loop'?

for idx, val in enumerate(files):
    self.Bind(wx.EVT_MENU, lambda event: self.loadFile(val), id=100+idx)

The above code always passes the last 'val' of the loop to the function for all binds.

ssv
  • 125
  • 1
  • 11
  • It is wx.Menu. Please see the below code. `menu1 = wx.Menu() for idx, val in enumerate(files): menu1.AppendItem(wx.MenuItem(menu1, 100+idx, ntpath.basename(val))) self.Bind(wx.EVT_MENU, lambda event: self.loadFile(val), id=100+idx)` I am trying to add menu items dynamically based on the files in a directory. – ssv Aug 28 '15 at 19:34
  • If it's not clear from the dupe target, try default variables in the lambda: `self.Bind(wx.EVT_MENU, lambda event, val=val: self.loadFile(val), id=100+idx)` – Kevin Aug 28 '15 at 19:52
  • @Kevin, it worked. Thanks a ton. – ssv Aug 28 '15 at 19:58

0 Answers0