[英]Making python/tkinter label widget update?
I'm working on getting a python/tkinter label widget to update its contents.我正在努力获取一个 python/tkinter 标签小部件来更新其内容。 Per an earlier thread today, I followed instructions on how to put together the widgets.
根据今天早些时候的一个线程,我按照有关如何组合小部件的说明进行操作。 At runtime, however, the label widget does NOT change contents, but simply retains its original content.
然而,在运行时,标签小部件不会更改内容,而只是保留其原始内容。 As far as I can tell, decrement_widget() is never called at all.
据我所知,decrement_widget() 从来没有被调用过。 Any ideas?
有什么想法吗?
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()
You'll want to set the label's textvariable
with a StringVar
;您需要使用
StringVar
设置标签的textvariable
; when the StringVar
changes (by you calling myStringVar.set("text here")
), then the label's text also gets updated.当
StringVar
更改时(通过调用myStringVar.set("text here")
),标签的文本也会更新。 And yes, I agree, this is a strange way to do things.是的,我同意,这是一种奇怪的做事方式。
See the Tkinter Book for a little more information on this:有关这方面的更多信息,请参阅Tkinter Book :
You can associate a Tkinter variable with a label.
您可以将 Tkinter 变量与标签相关联。 When the contents of the variable changes, the label is automatically updated:
当变量的内容发生变化时,标签会自动更新:
v = StringVar() Label(master, textvariable=v).pack() v.set("New Text!")
I think you're getting a "referenced before assignment" error because Python thinks remaining
is in the local scope.我认为您会收到“分配前引用”错误,因为 Python 认为
remaining
在本地范围内。
In Python 3, you can say nonlocal remaining
.在 Python 3 中,您可以说
nonlocal remaining
。 But in Python 2, I don't believe there's a way to refer to a non-local, non-global scope.但是在 Python 2 中,我不相信有一种方法可以引用非本地、非全局范围。 This worked for me:
这对我有用:
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()
To update text in a label you can try the following:要更新标签中的文本,您可以尝试以下操作:
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()
import tkinter
from tkinter import *
# just init some vars
remaining = 0
secs = 0
root = tkinter.Tk()
prompt = StringVar()
def snooze (secs):
"""
Snoozes for the given number of seconds. During the snooze, a progress
dialog is launched notifying the
"""
def decrement_label ():
global remaining, prompt
remaining -= 1
prompt.set('Snoozing %d sec(s)' % remaining)
label1.update_idletasks()
if not remaining:
print("end ... ")
root.destroy()
global remaining
prompt.set("hello")
label1 = tkinter.Label(root, textvariable=prompt, width=30)
label1.pack()
remaining = secs
for i in range(1, secs + 1):
root.after(i * 1000, decrement_label )
snooze(10)
root.mainloop()
I think you have to call snooze(secs)
function我认为你必须调用
snooze(secs)
函数
After that if your code again not works try this之后,如果您的代码再次不起作用,请尝试此操作
Set a variable设置变量
Variable = StringVar()
In the label widget you can set "textvariable" argument to the above mentioned "Variable".在标签小部件中,您可以将“textvariable”参数设置为上述“变量”。
Eg: label1 = Label(root,textvariable = Variable).pack()
例如:
label1 = Label(root,textvariable = Variable).pack()
And you can update by setting a new value to "Variable"您可以通过将新值设置为“变量”来更新
Eg: Variable.set("hi")
例如:
Variable.set("hi")
Hope you got it !!!希望你明白!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.