How to make an array of buttons which change text upon clicking using Tkinter : <p>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:</p>

<pre><code>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()
</code></pre>

<p>However, the resulting window looks like this:</p>

<p><a href="https://i.stack.imgur.com/Vl6N5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vl6N5.png" alt="enter image description here"></a></p>

<p>and the buttons don't change when clicked. Any ideas why this does not work?</p>
