[英]Displaying time in tkinter label
我試圖讓當前時間在 tkinter label 中每秒顯示和更新。
我假設我這樣做完全錯誤。 我對此很陌生,並試圖一點一點地解決它。
from tkinter import *
import sys
import time
#function to close window when ESC is pressed
def close(event):
sys.exit()
def curtime():
while 1==1:
current_time = time.strftime('%H:%M:%S')
print(current_time)
time.sleep(1)
#Main System
window = Tk()
window.state('zoomed')
window.configure(bg='#3a3a3a')
#Frames
top_frame = Frame(window)
top_frame.pack(side=TOP)
bottom_frame = Frame(window)
bottom_frame.pack(side=BOTTOM)
#Labels
header_label = Label(top_frame, text="SARA", background='#3a3a3a', fg='#ffffff', font=("calibri", 50))
header_label.pack(side=LEFT)
header_label2 = Label(top_frame, textvariable = curtime(), background='#3a3a3a', fg='#ffffff', font=("calibri", 50))
header_label2.pack(side=LEFT)
#on press ESC window closes
window.bind('<Escape>', close)
window.mainloop()
#function to get timer to calculate
def timer():
timer_tick = strftime("%I:%M:%S")
time_label.configure(text=timer_tick)
time_label.after(1000, timer)
time_label = Label(top_frame, background='#3a3a3a', fg='#ffffff', font=("calibri", 50))
time_label.pack(side=LEFT)
#if statement which constantly returns true to make the timer refresh and tick
if __name__ == "__main__":
timer()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.