更新时间:2023-02-26 17:12:39
使用线程可以几乎(不完全)与系统时钟同步:
Using thread can be almost (not completely) in sync with system clock:
from datetime import datetime
import threading
import time
import tkinter as tk
root = tk.Tk()
clock = tk.StringVar()
tk.Label(root, textvariable=clock, font="Times 20 bold", fg='light green', bg="dark green").pack()
def tick():
while True:
now = datetime.now()
clock.set(now.strftime("%T"))
time.sleep(1-now.microsecond/1000000)
threading.Thread(target=tick, daemon=True).start()
root.mainloop()