Leonardo Scotti
Leonardo Scotti

Reputation: 1080

How to Correctly Bind wx.MenuItems not stored in a variable

Premise:

I'm creating a little wx.Frame were i have a wx.SubMenu with a variable number of wx.MenuItems, and i want to bind they to a method, so i used this code:

self.selectworkout = wx.Menu()
self.x = 110
y = []
for elem in self.workout_list:
    y.append(self.selectworkout.Append(int(self.x), elem.title, elem.title + " | " + elem.description))
    index = self.workout_list.index(elem)
    self.Bind(wx.EVT_MENU, lambda event: self.OnSelectWorkout(event, elem), y[index])
    self.x+=1

to have a pointer to the wx.MenuItems, i stored them in a list and then i bind they using the corrispondent list item

Issue:

The problem is that when i click them, the call the method but they pass the same parameters, like if i have been bindin the same wx.MenuItem

Question:

Do you know where th problem is or what is the optimal way to do what i need?

Upvotes: 0

Views: 107

Answers (1)

Michael Butscher
Michael Butscher

Reputation: 10959

The problem with

self.Bind(wx.EVT_MENU, lambda event: self.OnSelectWorkout(event, elem), y[index])

is that the elem in the lambda expression is actually the variable from the surrounding function. If it is changed by the for-loop it is changed for all created lambdas.

Simple solution: Change it to:

self.Bind(wx.EVT_MENU, lambda event, elem=elem: self.OnSelectWorkout(event, elem), y[index])

Upvotes: 1

Related Questions