0

I'm trying to build a menu out of given array, where the label and command are based on the array item.

arr1 = ["item0","item1","item2"]
for arg in arr1:
    self.menu.add_command(label=arg, command= lambda: print(arg))

With the above code the labels works fine, but all the entries commands are printing the same text of the last arg (item2).

OB1
  • 3
  • 1

1 Answers1

0

There is a simple workaround to that: Use lambda like this:

self.menu.add_command(label=arg, command= lambda x=arg: print(x))
Thalis
  • 188
  • 2
  • 12