[英]How to make a timer count down to a time you assign for it python
我正在嘗試制作一個倒計時到特定時間的程序。 在此程序中,我指定了要單擊“我”的時間,但是我在弄清楚如何制作倒計時的計時器時遇到了麻煩。 這是我目前擁有的代碼:
import time
from tkinter import *
from datetime import datetime
from threading import Timer
tk = Tk()
canvas = Canvas(tk, width=400, height=400)
canvas.pack()
x = datetime.today()
y = x.replace(day=x.day, hour=1, minute=30, second=0, microsecond=0)
delta_t = y-x
secs = delta_t.seconds+1
def hello_world():
label = Label(tk, text="CLICK NOW", font=('Times', 45), fg='blue')
label.place(relx=0.5, rely=0.5, anchor=CENTER)
t = Timer(secs, hello_world)
t.start()
tk.mainloop()
如果有人對計時器倒計時到指定時間有任何建議,將不勝感激。 預先感謝您的任何幫助
不行嗎
secs = delta_t.total_seconds() + 1
代替
secs = delta_t.seconds + 1
這是一個使用tkinter制作的非常簡單的計時器,您可以將其合並到代碼中:只需將其用作.after()
from tkinter import *
root = Tk()
msg = Label(root, text = str(60))
msg.pack()
def timer():
msg['text']=str(int(msg['text'])-1)
root.after(60, timer) # continue the timer
root.after(60, timer) # 60 ms = 1 second
root.mainloop()
這是使用tkinter制作計時器的最基本概念。 這將非常有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.