How can I identify buttons, created in a loop?

后端 未结 4 1057
借酒劲吻你
借酒劲吻你 2020-12-06 15:29

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 but

4条回答
  •  粉色の甜心
    2020-12-06 15:58

    Try modifying your code as below:

    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
    

    and:

    def delete(self, i, j):
        self.b[i][j].destroy()
    

提交回复
热议问题