我試著做一個圣誕節倒數計時器,我快完成了。我運行了這段代碼,它運行了一秒鐘,然后關閉了所有內容,沒有錯誤。我在 python 方面不太先進,所以如果我犯了任何愚蠢的錯誤,那就是原因。可能來自執行緒嗎?我對執行緒真的很陌生,所以可能是這樣。
繼承人我的代碼現在。
import datetime
from tkinter import *
import time
from math import *
import threading
import datetime
import asyncio
import sys
root = Tk()
sys.setrecursionlimit(int(10e 6))
grid_placeholder = Label(root, text = " ", font=("Arial", 20))
grid_placeholder.grid(row = 1, column = 0)
seconds_check = ""
def xm():
dt = datetime.datetime
now = dt.now()
xmas_days = dt(year = 2021, month = 12, day = 25) - dt(year=now.year, month=now.month, day=now.day, hour=now.hour, minute=now.minute, second=now.second)
string_xmas1 = ""
string_xmas1 = string_xmas1 str(xmas_days)
hours = string_xmas1.rsplit(':', 2)[0]
hours = hours " hours, "
minutes = string_xmas1.rsplit(':', 2)[1]
minutes = minutes " minutes, "
seconds = string_xmas1.rsplit(':', 1)[1]
seconds = seconds " seconds"
xmas_days = hours minutes seconds
if xmas_days != seconds_check:
display_time = Label(root, text = xmas_days, font=("Arial", 13))
display_time.grid(row = 1, column = 0)
grid_placeholder.grid(row = 1, column = 0)
xmas_days = seconds_check
xm()
else:
display_time.destroy()
display_time = Label(root, text = xmas_days, font=("Arial", 13))
display_time.grid(row = 1, column = 0)
grid_placeholder.grid(row = 1, column = 0)
xm()
t1 = threading.Thread(target = xm)
t1.daemon = True
def thread():
t1.start()
button_time = Button(root, text= 'CLICK FOR COUNTDOWN', command = thread, height = 5, width = 20)
button_time.grid(row = 0, column = 0)
root.mainloop()
uj5u.com熱心網友回復:
您不需要在這里使用執行緒。
在 tkinter 中,您可以after()在一段時間后使用方法呼叫函式:
def xm():
# calculations and display
root.after(1000, xm)
button_time = Button(root, text= 'CLICK FOR COUNTDOWN', command = xm, height = 5, width = 20)
button_time.grid(row = 0, column = 0)
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/356590.html
