简体   繁体   English

如何使计时器倒数至您为其分配的时间python

[英]How to make a timer count down to a time you assign for it python

I am trying to make a program which counts down to a specific time. 我正在尝试制作一个倒计时到特定时间的程序。 In this program I have assigned the time when I want it to say click me but I am having trouble figuring out how to make a timer which counts down to that time. 在此程序中,我指定了要单击“我”的时间,但是我在弄清楚如何制作倒计时的计时器时遇到了麻烦。 This is the code I currently have: 这是我目前拥有的代码:

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()

If anyone has any suggestions to have the timer countdown to the specified time it would be greatly appreciated. 如果有人对计时器倒计时到指定时间有任何建议,将不胜感激。 Thanks in advance for any help 预先感谢您的任何帮助

Shouldn't you do? 不行吗

secs = delta_t.total_seconds() + 1

Instead of 代替

secs = delta_t.seconds + 1

Here's a very simple timer made using tkinter that you can incorporate into your code: just use it as .after() 这是一个使用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()

This is the most basic concept of making a timer using tkinter. 这是使用tkinter制作计时器的最基本概念。 And it will be very useful. 这将非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM