def run_game(self):
root = Tk()
for i in range(0, 6):
for j in range(0, len(self.board[i])):
button = Button(root, text="0", height=2, width=10, command=lambda: self.if_clicked_square(i,j))
button.place(x=25 + (100 * j), y=100 + (100 * i))
# button.pack()
self.buttonsList[i].append([button])
root.mainloop()
The above function is creating a GUI board consisting of 28 buttons using Tkinter. I used nested for loops to create every button and place it on the board and also in a list of buttons. The problem arises when setting the command for each button. I want to use the function if_clicked_square(i,j)
for the command. The function uses the i and j indices to operate. The problem is that the command for every single button after the loop is done is if_clicked_square(5,2)
, which happen to be the last indices of the loop. I want every button to have a unique command based on the indices of the for loop. For example, one of the buttons should have the command if_clicked_square(0,0)
, one should have the command if_clicked_square(1,0)
, and so forth for all 28 buttons. Can I get any tips to accomplish this?