I'm trying to implement a simple GUI for the game of Tic Tac Toe using Tkinter. As a first step, I'm trying to make an array of buttons which change from being unlabeled to having the "X" label when clicked. I've tried the following:
import Tkinter as tk
class ChangeButton:
def __init__(self, master, grid=np.diag(np.ones(3))):
frame = tk.Frame(master)
frame.pack()
self.grid = grid
self.buttons = [[tk.Button()]*3]*3
for i in range(3):
for j in range(3):
self.buttons[i][j] = tk.Button(frame, text="", command=self.toggle_text(self.buttons[i][j]))
self.buttons[i][j].grid(row=i, column=j)
def toggle_text(self, button):
if button["text"] == "":
button["text"] = "X"
root = tk.Tk()
root.title("Tic Tac Toe")
app = ChangeButton(root)
root.mainloop()
However, the resulting window looks like this:
and the buttons don't change when clicked. Any ideas why this does not work?