I have created a list and a cycle that adds new buttons to that list. The only problem is that the buttons don't seem to remember their appropriate command and always have to call back to the variable x
in the cycle.
Buttons = []
for x in range(9):
Buttons.append(Button(frame2, text = str(x+1), command = lambda : num_pressed(x+1), width = 10, height = 5))
The function num_pressed
uses the variable x
in the same way the text does, except that the text stays the same once it's defined but the command doesn't.
Therefore once the the buttons are created all of them the call the command for x = 8
as that is where the cycle ends. So in the end they all call the command num_pressed(9)
How do I make the command remember the value it is supposed to the same way the text does? Maybe assign some sort of value to each button and then assign the command based on that value that's stored in the button itself?
Sidenote: I haven't found a way but it should be possible to use the method .cget('text')
as the text of the button coincidentally has the same value as what should be the variable used in num_pressed
.