How can I identify buttons, created in a loop?
<p>I am trying to program a minesweeper game on python using tkinter. I started off by creating a grid of buttons using a two dimensional list of 10x10. Then I created each button using a loop just so I don't have to manually create every single button and clip them on. </p>

<pre><code>self.b=[[0 for x in range(1,12)] for y in range(1,12)] #The 2 dimensional list
for self.i in range(1,11):
     for self.j in range(1,11):
            self.b[self.i][self.j]=tkinter.Button(root,text = ("     "),command = lambda: self.delete()) # creating the button
            self.b[self.i][self.j].place(x=xp,y=yp) # placing the button
            xp+=26 #because the width and height of the button is 26
        yp+=26
        xp=0
</code></pre>

<p>Basically I want the button to disappear upon being pressed. The problem is that I don't know how to let the program delete specifically the button that I pressed, as all the buttons are exactly the same. When creating the delete function:</p>

<pre><code>def delete(self):
    self.b[???][???].destroy()
</code></pre>

<p>I don't know how to let the program know which button it was that the user presses, so it can delete that specific one.</p>

<p><strong>The question:</strong>
Is there a way to let each button have something unique that allows it to be differentiated from the other buttons? Say assign each button a specific coordinate, so when button (2,3) is pressed, the numbers 2 and 3 are passed onto the delete function, so the delete function can delete button (2,3)?</p>

<p>If you just want to destroy the Button widget, the simple way is to add the callback after you create the button. Eg,</p>

<pre><code>import Tkinter as tk

grid_size = 10

root = tk.Tk()
blank = " " * 3
for y in range(grid_size):
    for x in range(grid_size):
        b = tk.Button(root, text=blank)
        b.config(command=b.destroy)
        b.grid(column=x, row=y)

root.mainloop()
</code></pre>

<p>However, if you need to do extra processing in your callback, like updating your grid of buttons, it's convenient to store the Button's grid indices as an attribute of the Button object.   </p>

<pre><code>from __future__ import print_function
import Tkinter as tk

class ButtonDemo(object):
    def __init__(self, grid_size):
        self.grid_size = grid_size
        self.root = tk.Tk()
        self.grid = self.button_grid()
        self.root.mainloop()

    def button_grid(self):
        grid = []
        blank = " " * 3
        for y in range(self.grid_size):
            row = []
            for x in range(self.grid_size):
                b = tk.Button(self.root, text=blank)
                b.config(command=lambda widget=b: self.delete_button(widget))
                b.grid(column=x, row=y)
                #Store row and column indices as a Button attribute
                b.position = (y, x)
                row.append(b)
            grid.append(row)
        return grid

    def delete_button(self, widget):
        y, x = widget.position
        print("Destroying", (y, x))
        widget.destroy()
        #Mark this button as invalid 
        self.grid[y][x] = None

ButtonDemo(grid_size=10)
</code></pre>

<p>Both of these scripts are compatible with Python 3, just change the import line to</p>

<pre><code>import tkinter as tk
</code></pre>

python tkinter minesweeper