I'm trying to make a simple gameboard using Tkinter with buttons that will change a single item in a 2D array, that corresponds to its position in the window - so I can check if the game's over by checking the array. This is my code:
from tkinter import *
root = Tk()
board = []
for x in range(8):
board.append([0,0,0,0,0,0,0,0])
def toggle(row,column):
global board
board[row][column] = (board[row][column]+1)%2
print("-"*10)
for x in board:
for y in x:
print(y, end="")
print("")
print("-"*10)
for x in range(8):
for y in range(8):
button = Button(master=root, text="--", command=lambda:toggle(y,x))
button.grid(row=y,column=x)
root.mainloop()
However, this just ends up giving ALL the buttons the command toggle(7,7) and they all just toggle the final item of the final item in board. I tried an old question but I think it was python2, because the solution didn't change anything for my code.