I need to do a tic tac toe game, but I am stuck on how to change the value of a cell from "-" to "X" or "O",
I have been looking on internet since yesterday of how to tell the program "The user click on the cell [0][2]", I don't know how to tell the program which cell the user pressed.
This is the code that I have:
import tkinter as tk
buttons = []
window = tk.Tk()
frame_title = tk.Frame(master=window)
tk.Label(text="Tic tac toe", master=frame_title).pack()
frame_title.pack()
frame_body = tk.Frame(master=window)
frame_body.rowconfigure(0, minsize=50, weight=1)
frame_body.rowconfigure(1, minsize=50, weight=1)
frame_body.rowconfigure(2, minsize=50, weight=1)
frame_body.columnconfigure([0, 1, 2], minsize=50, weight=1)
for i in range(3):
for j in range(3):
new_button = tk.Button(master=frame_body, text="-", command=value_change)
new_button.grid(row=i, column=j, sticky="nsew")
frame_body.pack()
window.mainloop()
This is what this code make:
And this is the best try that I already did:
def value_change():
turn = 0
if turn == 0:
new_button["text"] = "X"
turn += 1
else:
new_button["text"] = "O"
turn -= 1
But this one, if I clicked on any cell, only change the value of the cell[2][2].
If I press [0] [2] I need to change the value of this cell
I am using the module tkinter cause this is what the teacher used in the class to teach how to make a counter that increase and decrease, but that was a lot easier cause if I click on the "+" cell[0][0] or "-"[0][2] it only changed the value of the number on the cell [zero][one]
If you know where I can read an example about how to do this with tkinter, I will be very grateful.