I am trying to create a clock in tkinter and I am attempting to update a label whenever the time changes however, I have no idea how to do this. I have tried the after method however I believe I am doing something wrong.
My code:
from tkinter import *
import time
from datetime import date
from datetime import datetime
date = date.today()
now = datetime.now()
time = now.strftime("%H:%M:%S")
root = Tk()
root.title("Clock")
root.geometry("500x500")
Time = Label(root, text = f"{time}", font = ("Times New Roman", 50))
Time.place(x = "160", y = "100")
def Update():
Time.config(text = f"{time}")
root.after(1000, Update())
root.mainloop()
Does anyone know what I am doing wrong?
First of all, root.after
takes a function, not the result of a function:
root.after(1000, Update)
Secondly, you need some way to make the update happen every second, rather than just once. The easiest way is to add a root.after
call to your Update
function, as well as calculating the new time string each time the function is called:
def Update():
now = datetime.now()
time = now.strftime("%H:%M:%S")
Time.config(text = f"{time}")
root.after(1000, Update)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.