How to give automatically generated buttons names in Tkinter?
<p>So i am genertaing a grid of 3x3 buttons for a Tic Tac Toe game im making, and i want to to end up so that when a button is pressed it changes to either a X or O, however i dont know how to give each button a unique identifier so i know which button to change.</p>

<p>Heres the code for the buttons.</p>

<pre><code>num=1
for row in range(3):
for column in range(3):
    Button(TTTGrid,bg="#ffffff",  width=20,height = 6, command=lambda row=row, column=column: TTTGridPress(row, column),relief=SUNKEN).grid(row=row, column=column, sticky=W)
    num=num+1
</code></pre>

<p>Use a dictionary or list. For example:</p>

<pre><code>buttons = {}
for row in range(3):
    for column in range(3):
        buttons[(row, column)] = Button(...)
        buttons[(row, column)].grid(...)
</code></pre>

<p>Later, you can refer to the button in row 2, column one as:</p>

<pre><code>buttons[(2 1)].configure(...)
</code></pre>

<hr>

<p>Note: you need to call <code>grid</code> (or <code>pack</code>, or <code>place</code>) in a separate statement, because they return <code>None</code>. If you do it on the same statement (eg: <code>Button(...).grid(...)</code>), the value that gets saved is <code>None</code> rather than the instance of the button.</p>

python tkinter