How do I create an automatically updating GUI using Tkinter?

40,579

Solution 1

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 datetime

root = 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 time
clock()

root.mainloop()

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


EDIT: (2022.01.01)

Updated to Python 3 with other changes suggested by PEP 8 -- Style Guide for Python Code

import tkinter as tk   # PEP8: `import *` is not preferred
import datetime

# --- functions ---
# PEP8: all functions before main code
# PEP8: `lower_case_name` for funcitons
# PEP8: verb as function's name
                     
def update_clock():
    # get current time as text
    current_time = datetime.datetime.now().strftime("Time: %H:%M:%S")
    
    # udpate text in Label
    lab.config(text=current_time)
    #lab['text'] = current_time
    
    # run itself again after 1000 ms
    root.after(1000, update_clock) 

# --- main ---

root = tk.Tk()

lab = tk.Label(root)
lab.pack()
    
# run first time at once
update_clock()

# run furst time after 1000ms (1s)
#root.after(1000, update_clock)

root.mainloop()

Solution 2

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.

Solution 3

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

Share:
40,579
RainingEveryday
Author by

RainingEveryday

Updated on January 02, 2022

Comments

  • RainingEveryday
    RainingEveryday over 2 years
    from Tkinter import *
    import time
    #Tkinter stuff
    
    class App(object):
        def __init__(self):
            self.root = Tk()
    
            self.labeltitle = Label(root, text="",  fg="black", font="Helvetica 40 underline bold")
            self.labeltitle.pack()
    
            self.labelstep = Label(root, text="",  fg="black", font="Helvetica 30 bold")
            self.labelstep.pack()
    
            self.labeldesc = Label(root, text="",  fg="black", font="Helvetica 30 bold")
            self.labeldesc.pack()
    
            self.labeltime = Label(root, text="",  fg="black", font="Helvetica 70")
            self.labeltime.pack()
    
            self.labelweight = Label(root, text="",  fg="black", font="Helvetica 25")
            self.labelweight.pack()
    
            self.labelspeed = Label(root, text="",  fg="black", font="Helvetica 20")
            self.labelspeed.pack()
    
            self.labeltemp = Label(root, text="", fg="black", font="Helvetica 20")
            self.labeltemp.pack()
    
            self.button = Button(root, text='Close recipe', width=25, command=root.destroy)
            self.button.pack()
    
        def Update(self, label, change):
            label.config(text=str(change))
    
    def main():
        app = App()
        app.mainloop()
    
    if __name__ == "__main__":
        main()
    

    I'm trying to create a recipe display which will show the step, instructions, weight and other variables on a screen in a Tkinter GUI.

    However, I do not know how to update the GUI to change with each new step of the recipe, as the content has to be dynamically updated based on user input (taken from a server). How can I achieve updating of the GUI's other elements based on the change in steps?

  • Marc
    Marc over 2 years
    I think this is wonderfully elegant. A great alternative with considerable creative possibility.