How do I create an automatically updating GUI using Tkinter? How do I create an automatically updating GUI using Tkinter? tkinter tkinter

How do I create an automatically updating GUI using Tkinter?


You can use after() to run function after (for example) 1000 miliseconds (1 second) to do something and update text on labels. This function can run itself after 1000 miliseconds again (and again).

It is example with current time

from Tkinter import *import datetimeroot = Tk()lab = Label(root)lab.pack()def clock():    time = datetime.datetime.now().strftime("Time: %H:%M:%S")    lab.config(text=time)    #lab['text'] = time    root.after(1000, clock) # run itself again after 1000 ms# run first timeclock()root.mainloop()

BTW: you could use StringVar as sundar nataraj Сундар suggested


if you want to change label dynamically

self.dynamiclabel=StringVar()self.labeltitle = Label(root, text=self.dynamiclabel,  fg="black", font="Helvetica 40 underline bold")self.dyanamiclabel.set("this label updates upon change")self.labeltitle.pack()

when ever you get new value then just use .set()

self.dyanamiclabel.set("Hurrray! i got changed")

this apply to all the labels.To know more read this docs


If you are using labels, then you can use this:

label = tk.Label(self.frame, bg="green", text="something")label.place(rely=0, relx=0.05, relwidth=0.9, relheight=0.15)refresh = tk.Button(frame, bg="white", text="Refreshbutton",command=change_text) refresh.pack(rely=0, relx=0.05, relwidth=0.9, relheight=0.15)def change_text()   label["text"] = "something else"

Works fine for me, but it is dependent on the need of a button press.