使python/tkinter标签小部件更新?
我正在准备一个python/tkinter标签小部件来更新它的内容。根据今天早些时候的一篇文章,我遵循了关于如何组合小部件的说明。但是,在运行时,标签小部件不会更改内容,只会保留其原始内容。据我所知,decrement_widget()从未被调用过。有什么想法吗使python/tkinter标签小部件更新?,python,tkinter,Python,Tkinter,我正在准备一个python/tkinter标签小部件来更新它的内容。根据今天早些时候的一篇文章,我遵循了关于如何组合小部件的说明。但是,在运行时,标签小部件不会更改内容,只会保留其原始内容。据我所知,decrement_widget()从未被调用过。有什么想法吗 def snooze (secs): """ Snoozes for the given number of seconds. During the snooze, a progress dialog is launched
def snooze (secs):
"""
Snoozes for the given number of seconds. During the snooze, a progress
dialog is launched notifying the
"""
root = Tkinter.Tk()
prompt = 'hello'
label1 = Tkinter.Label(root, text=prompt, width=len(prompt))
label1.pack()
remaining = secs
def decrement_label ():
text = "Snoozing %d sec(s)" % remaining
remaining -= 1
label1.config(text=text, width=100)
label1.update_idletasks()
for i in range(1, secs + 1):
root.after(i * 1000, decrement_label )
root.after((i+1) * 1000, lambda : root.destroy())
root.mainloop()
您需要使用一个参数设置标签的
textvariable
;当StringVar
更改时(通过调用myStringVar.set(“此处的文本”)
),标签的文本也会更新。是的,我同意,这是一种奇怪的做事方式
有关这方面的更多信息,请参阅:
可以将Tkinter变量与标签相关联。变量内容更改时,标签将自动更新:
v = StringVar()
Label(master, textvariable=v).pack()
v.set("New Text!")
我认为您得到了一个“分配前引用”错误,因为Python认为剩余
在本地范围内
在Python3中,可以说非局部剩余
。但在Python2中,我认为没有一种方法可以引用非局部、非全局范围。这对我很有用:
remaining = 0
def snooze (secs):
"""
Snoozes for the given number of seconds. During the snooze, a progress
dialog is launched notifying the
"""
global remaining
root = Tkinter.Tk()
prompt = 'hello'
label1 = Tkinter.Label(root, text=prompt, width=len(prompt))
label1.pack()
remaining = secs
def decrement_label ():
global remaining
text = "Snoozing %d sec(s)" % remaining
remaining -= 1
label1.config(text=text, width=100)
label1.update_idletasks()
for i in range(1, secs + 1):
root.after(i * 1000, decrement_label )
root.after((i+1) * 1000, lambda : root.destroy())
root.mainloop()
要更新标签中的文本,可以尝试以下操作:
from tkinter import *
root = Tk()
root.title("Title")
root.geometry('300x300')
def clear_text(self):
txtE.delete(0, 'end')
def new_label(event=None):
Entree = txtE.get()
lbl1['text'] = Entree.title()
clear_text(txtE)
lbl1 = Label(root, text='Hello There')
lbl1.pack()
txtE = Entry(root)
txtE.focus()
txtE.pack()
Button(root, text='Enter', command=new_label).pack()
Button(root, text='Quit', command=root.destroy).pack(side=BOTTOM)
root.bind('<Return>', new_label)
root.mainloop()
从tkinter导入*
root=Tk()
根标题(“标题”)
根几何体('300x300')
def清除文本(自身):
txtE.delete(0,“结束”)
def新_标签(事件=无):
entre=txtE.get()
lbl1['text']=entre.title()
清除文本(txtE)
lbl1=标签(根,text='Hello There')
lbl1.pack()
txtE=条目(根)
txtE.focus()
txtE.pack()
按钮(root,text='Enter',command=new_label).pack()
按钮(root,text='Quit',command=root.destroy).pack(side=BOTTOM)
root.bind(“”,新的\u标签)
root.mainloop()
我想你必须调用snooze(secs)
函数
之后,如果您的代码再次无法工作,请尝试以下操作
设置一个变量
Variable = StringVar()
在标签小部件中,您可以将“textvariable”参数设置为上述“Variable”
例如:
label1=Label(root,textvariable=Variable).pack()
您可以通过将新值设置为“变量”进行更新
例如:Variable.set(“hi”)
希望你得到它 根据您的建议,我将递减标签函数更改为'text=。。。;剩余-=1;sv=Tkinter.StringVar();sv.Set(文本);label1.conf(text=sv,width=100);label1.update_idletasks()。但是,这对运行时行为没有任何影响。减量标签函数似乎根本没有被调用!我还发现“剩余-=1”语句似乎是导致问题的原因。Python在遇到该语句时会以某种方式默默地失败。是否有问题?创建
标签时必须设置StringVar
,然后修改StringVar
的同一实例。您不需要反复调用label1.conf
。发布完全更新的代码。注意:使用StringVar并不是严格要求的。只需使用configure
:l=label(…);…;直接更新标签小部件即可。。。;l、 配置(text=“new text”)
,这样可以减少一个要管理的对象。
Variable = StringVar()