Skip to main content
 首页 » 编程设计

python之按下按钮时更新 Tkinter 中的标签

2025年05月04日15wayfarer

我想制作一个“程序”,当您按下按钮并打印出一个变量时更新标签,但是使用我的代码它不起作用。有人可以帮帮我吗?

提前致谢!

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()(doesn't do anything close to what you think it does),reconfigure the widget使用 label_1.config(text=x)。