Start New Discussion within our Software Development Community

Heyho users;

I need to update a label in a window every x seconds to get an variabel ("res") which is up to date.
My current code:

def init(win):
    win.title("Ausgangsposition")
    win.minsize(800, 600)
    lab=Label(win, text="Aktuell: " + res +" Euro", bg='#40E0D0', fg='#FF0000')
    lab.place(x=20, y=30)
    btn.pack()

def updater(win):
    ids = lab.after(1000, updater)
    print ("Done")  

(I don't post the complete code, I think this is enough to understand)
Nothing happens with the code above.
Any idea how to fix?
I need to stop the updater when window "wín" is being closed, how to do this, because I don't want to get errors in cmd.

All the best;

There is more than one way to skin that cat. I use a tkinter variable and update it. Note that you have to call updater() to get the whole thing started.

try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x

class UpdateLabel():
    def __init__(self):
        self.win = tk.Tk()
        self.win.title("Ausgangsposition")
        self.win.minsize(800, 600)
        self.ctr = 0
        self.tk_var = tk.StringVar()
        self.tk_var.set("0")
        lab=tk.Label(self.win, textvariable=self.tk_var,
                       bg='#40E0D0', fg='#FF0000')
        lab.place(x=20, y=30)
        self.updater()
        self.win.mainloop()

    def updater(self):
        self.ctr += 1
        self.tk_var.set(str(self.ctr))
        if self.ctr < 10:
            self.win.after(1000, self.updater)
        else:
            self.win.quit()

UL=UpdateLabel()
This article has been dead for over six months. Start a new discussion instead.