gpt4 book ai didi

python - 通过单击按钮更改按钮文本不起作用

转载 作者:行者123 更新时间:2023-12-01 08:40:24 27 4
gpt4 key购买 nike

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

button_list = []

def create_buttons():
# Create the buttons
for x in range(code_squares):
# Code_squares is how many squares are on the board
new_button = Button(frame_list[x], text = "", relief = RAISED)
new_button.pack(fill=BOTH, expand=1)
new_button.bind("<Button-1>", lambda event: box_open(event, x))
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.
print("in")
button_list[x].config(text=box_list[x], relief = SUNKEN)

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

最佳答案

我相信问题出在您尝试将 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 = []

关于python - 通过单击按钮更改按钮文本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53530929/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com