使用按钮单击更改按钮文本不起作用

时间:2018-11-29 02:21:28

标签: python button tkinter minesweeper

我正在编程Minesweeper。我已经完成了所有逻辑,现在我正在做GUI。我正在使用Tkinter。板上有这么多的空间,我想自动创建所有这些按钮,这种方式已经完成:

Spark-SQL

print语句只是一个测试。当我单击一个点时,它将执行打印语句,因此我知道它到达了该位置,但不会更改按钮文本。感谢所有帮助!

1 个答案:

答案 0 :(得分:0)

我认为问题在于您试图将x嵌入lambda中的方法,请尝试尝试一下是否可以解决您的问题:

from functools import partial

def create_buttons():
    for n in range(code_squares):
        # Code_squares is how many squares are on the board
        new_button = Button(frame_list[n], text="", relief=RAISED)
        new_button.pack(fill=BOTH, expand=1)
        new_button.bind("<Button-1>", partial(box_open, x=n))
        button_list.append(new_button)

def box_open(event, x):
    if box_list[x] == "M":
        # Checks if the block is a mine
        button_list[x].config(text="M", relief=SUNKEN)
        # Stops if it was a mine
        root.quit()
    else:
        # If not a mine, it changes the button text to the xth
        # term in box_list, which is the amount of nearby mines.
        button_list[x].config(text=box_list[x], relief=SUNKEN)

button_list = []
相关问题