我试图使它,以便当用户点击一个按钮,它成为“X”或“0”(取决于他们的团队)。如何才能使按钮上的文本更新?到目前为止,我最好的办法是删除按钮,然后再次打印,但这只会删除一个按钮。以下是我目前掌握的情况:
from tkinter import *
BoardValue = ["-","-","-","-","-","-","-","-","-"]
window = Tk()
window.title("Noughts And Crosses")
window.geometry("10x200")
v = StringVar()
Label(window, textvariable=v,pady=10).pack()
v.set("Noughts And Crosses")
def DrawBoard():
for i, b in enumerate(BoardValue):
global btn
if i%3 == 0:
row_frame = Frame(window)
row_frame.pack(side="top")
btn = Button(row_frame, text=b, relief=GROOVE, width=2, command = lambda: PlayMove())
btn.pack(side="left")
def PlayMove():
BoardValue[0] = "X"
btn.destroy()
DrawBoard()
DrawBoard()
window.mainloop()
总结这条线索。
button.config
和button.configure
都工作了!。与标签一样,Button小部件也有一个
textvariable=
选项。您可以使用StringVar.set()
更新按钮。最小示例:btn
只是一个值字典,让我们看看接下来会发生什么:如您所见,它是一个很大的值字典,因此如果您想更改任何按钮,只需执行以下操作:
真的是这样!
它会自动更改按钮上的文本,即使您处于空闲状态!
相关问题 更多 >
编程相关推荐