Making python/tkinter label widget update?

Making python/tkinter label widget update?

Youll want to set the labels textvariable with a StringVar; when the StringVar changes (by you calling myStringVar.set(text here)), then the labels text also gets updated. And yes, I agree, this is a strange way to do things.

See the Tkinter Book for a little more information on this:

You can associate a Tkinter variable with a label. 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 youre getting a referenced before assignment error because Python thinks remaining is in the local scope.

In Python 3, you can say nonlocal remaining. But in Python 2, I dont believe theres a way to refer to a non-local, non-global scope. 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()

Making python/tkinter label widget update?

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

Leave a Reply

Your email address will not be published. Required fields are marked *