按下按钮时更新 Tkinter 中的标签 [英] Update a label in Tkinter when pressing a button
本文介绍了按下按钮时更新 Tkinter 中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想制作一个程序",当您按下按钮并打印出一个变量时,它会更新标签,但是使用我拥有的代码它不起作用.有人可以帮我吗?
I want to make a "program" that updates a label when you press a button and print out a variable, but with the code i have it does not work. Can someone help me out?
提前致谢!
from Tkinter import *
root = Tk()
x = 0
def test():
global x
x += 1
label_1.update()
label_1 = Label(root, text=x)
button_1 = Button(root, text='Click', command=test)
button_1.grid(row=0, column=0)
label_1.grid(row=0, column=1)
root.mainloop()
推荐答案
代替 label_1.update()
(其中 没有做任何与您认为的相似的事情), 使用 label_1.config(text=x)
重新配置小部件.
Instead of label_1.update()
(which doesn't do anything close to what you think it does), reconfigure the widget with label_1.config(text=x)
.
这篇关于按下按钮时更新 Tkinter 中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文