I have an array of buttons, that i would like to disable when pressed. But no matter which button I press, the last button only gets disabled. anyone would be able to guide me with the right solution.
buttons_text_var = [StringVar() for i in range(25)]
def set_button_function(button_index):
buttons[button_index].config(state="disabled")
buttons = [Button(window,textvariable = buttons_text_var[i],command=lambda : set_button_function(i), height=button_height, bd=button_border_size,
font=(button_text_font, button_text_size), width=button_width) for i in range(25)]
If i press a specific button, i want it to be disabled. but only the last button gets disabled. no matter which button is pressed
Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
You’ll need to modify the section with the lambda to use i=i
buttons = [Button(window,textvariable = buttons_text_var[i],command=lambda i=i : set_button_function(i), height=button_height, bd=button_border_size,
font=(button_text_font, button_text_size), width=button_width) for i in range(25)]
Failing to do this means that the lamda function will always use the last value.