制作python / tkinter标签小部件更新?

时间:2009-12-16 21:47:53

标签: python tkinter

我正在努力获取python / tkinter标签小部件来更新其内容。根据今天的早期帖子,我按照如何组合小部件的说明进行操作。但是,在运行时,标签小部件不会更改内容,而只是保留其原始内容。据我所知,从来没有调用decrement_widget()。有什么想法吗?

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()

5 个答案:

答案 0 :(得分:22)

您需要使用StringVar设置标签textvariable;当StringVar更改时(通过调用myStringVar.set("text here")),标签的文本也会更新。是的,我同意,这是一种奇怪的做事方式。

有关详细信息,请参阅the Tkinter Book

  

您可以将Tkinter变量与标签相关联。当变量的内容发生变化时,标签会自动更新:

v = StringVar()
Label(master, textvariable=v).pack()

v.set("New Text!")

答案 1 :(得分:6)

我认为你得到了一个“在赋值前引用”错误,因为Python认为remaining在本地范围内。

在Python 3中,您可以说nonlocal remaining。但是在Python 2中,我不相信有一种方法可以引用非本地的非全局范围。这对我有用:

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()

答案 2 :(得分:0)

    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()

答案 3 :(得分:0)

要更新标签中的文本,您可以尝试以下操作:

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()

答案 4 :(得分:0)

我认为您必须调用snooze(secs)函数

之后,如果您的代码再次不起作用,请尝试

设置变量

Variable = StringVar()

在标签小部件中,您可以将“ textvariable”参数设置为上述“ Variable”。

例如: label1 = Label(root,textvariable = Variable).pack()

您可以通过将新值设置为“变量”进行更新

例如: Variable.set("hi")

希望你知道了!