Search code examples
pythontkinterweatherlocaltimesystem-clock

How to fix the non-updating clock on a tkinter weather app?


Clock on tkinter doesn't work

I created a weather app, but the clock just constantly stand still the time, how to fix it???

current = datetime.now(home)
real = current.strftime("%d/%m/%Y\n%H:%M:%S %p")
clock.config(text=real)
root.after(1000, clock)


root = Tk()
root.title("Weather App")
root.geometry("910x500") 
root.resizable(0, 0)

root.mainloop()

Here's how I do it I would like the clock to change its minute, I tried but I can't It just stand still and doesn't change -_-your text


Solution

  • .after calls a function after a certain time , so the second argument that you give to .after is a function , for example :

    def update_time():
        current = datetime.now(home)
        real = current.strftime("%d/%m/%Y\n%H:%M:%S %p")
        clock.config(text=real)
        root.after(1000, update_time) #<-- after 1 second the function get called again
    
    
    root = Tk()
    root.title("Weather App")
    root.geometry("910x500") 
    root.resizable(0, 0)
    update_time() #<-- calls the function for the first time so the loop can start
    
    root.mainloop()
    

    so you call the function for the first time to start the loop and then the function calls itself after 1 second to update the time