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>Try modifying your code as below:</p>

<pre><code>self.b=[[0 for x in range(10)] for y in range(10)] #The 2 dimensional list
xp = yp = 0
for i in range(10):
    for j in range(10):
        self.b[i][j]=tkinter.Button(root,text="     ",command=lambda i=i,j=j: self.delete(i,j)) # creating the button
        self.b[i][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>and:</p>

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

python tkinter minesweeper