单击按钮时如何禁用它?

时间:2019-03-24 17:51:20

标签: python button tkinter

我是python的新手,自学成才。我正在尝试制作一款名为“ Mines”的简化扫雷游戏。该游戏有5 x 5的按钮网格,带有随机数量的地雷。除了一个问题,一切都工作正常。单击安全空间后,应禁用该按钮,这样您将无法再获得任何积分。用我的代码,我不知道该怎么做。

我确实已经尝试调用一种方法来禁用按钮,以及使用DISABLED命令。两者都不起作用。我希望单击后禁用“白色”按钮,因为这些是安全空间。在理想情况下,单击按钮时将调用disable()方法,并使用该方法禁用该按钮。

##Create the canvas of the board.
window = Tk ()
window.title("Mines Game")
window.geometry('819x655')
##Scoring function. The first box clicked awards 1 point, and after that each box is worth double your total points.
def score():
    global scores
    if (scores == 0):
        scores = scores + 1
        print ("Safe space hit! You have "   + str(scores) + " point.")
    else:
        scores = scores * 2 
        print ("Safe space hit! You have "   + str(scores) + " points.")
def squares():
    ##Main method of creating a 5x5 square of buttons.
    global scores
    scores = 0
    less_bombs = True
    r = -1
    c = 0
    x = 0
    if less_bombs:
        for i in range(5):
            r = r + 1
            for l in range(5):
                y = randint(0,25)
                if x == 5:
                    y = 10
                if y < 6:
                    btn = Button(window, bg = "red", command=end)
                    btn.grid(column = c,row = r)
                    #btn.grid(sticky="nesw")
                    btn.config(height = 8, width = 22)
                    x = x + 1
                else:
                    btn = Button(window, bg = "white", command=lambda:[score(),DISABLED])
                    btn.grid(column = c,row = r)
                    #btn.grid(sticky="nesw")
                    btn.config(height = 8, width = 22)
                c = c + 1
            c = 0
def end():
    ##This method creates the popup after you click a mine.
    end = Tk ()
    end.title ('Game Over!')
    end.geometry ('300x161')
    btn = Button(end, text ="Close game", command=close)
    btn.grid(column = 0,row = 0)
    #btn.grid(sticky="nesw")
    btn.config(height = 10, width = 20)
    btn = Button(end, text ="Reset game", command=lambda:[reset(),end.destroy()])
    btn.grid(column = 1,row = 0)
    #btn.grid(sticky="nesw"
    btn.config(height = 10, width = 20)
    if (scores == 1):
        print ("Game over! You hit a mine :(")
        print ("Your score for that game was "  + str(scores) + " point.")
    if (scores != 1):
        print ("Game over! You hit a mine :(")
        print ("Your score for that game was "  + str(scores) + " points.")
def disable():
    pass
def close():
    sys.exit()
def reset():
    squares()
squares()
window.mainloop()

我希望在单击按钮后将其禁用,但是当前玩家可以根据需要多次单击按钮。

1 个答案:

答案 0 :(得分:0)

您可以通过更改白色Buttons的创建来解决此问题,如下面所有大写字母所示。这为lambda函数提供了btn的默认值,该默认值会更改循环的每次迭代(否则该函数将始终引用在for循环中创建的最后一个)。

def squares():
    """ Main method of creating a 5x5 square of buttons. """
    global scores
    scores = 0
    less_bombs = True
    r = -1
    c = 0
    x = 0
    if less_bombs:
        for i in range(5):
            r = r + 1
            for l in range(5):
                y = randint(0,25)
                if x == 5:
                    y = 10
                if y < 6:
                    btn = Button(window, bg="red", command=end)
                    btn.grid(column=c, row=r)
                    #btn.grid(sticky="nesw")
                    btn.config(height=8, width=22)
                    x = x + 1
                else:
                    btn = Button(window, bg="white")  ## REMOVE command= OPTION. ##
                    btn.grid(column=c, row=r)
                    #btn.grid(sticky="nesw")
                    btn.config(height=8, width=22, # USE command SHOWN BELOW. ##
                        command=lambda btn=btn: [score(), btn.config(state=DISABLED)])
                c = c + 1
            c = 0