【问题标题】:How to make a timer count down to a time you assign for it python如何使计时器倒计时到您为其分配的时间python
【发布时间】:2017-08-13 18:13:44
【问题描述】:

我正在尝试制作一个倒计时到特定时间的程序。在这个程序中,我已经分配了我希望它说点击我的时间,但我无法弄清楚如何制作一个倒计时到那个时间的计时器。这是我目前拥有的代码:

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

如果有人对将计时器倒计时到指定时间有任何建议,我们将不胜感激。在此先感谢您的帮助

【问题讨论】:

标签: python canvas time tkinter timer


【解决方案1】:

你不应该这样做吗?

secs = delta_t.total_seconds() + 1

代替

secs = delta_t.seconds + 1

【讨论】:

  • 感谢您的建议。它似乎使它更有效地工作,但我如何让它倒计时到那个时间
【解决方案2】:

这是一个使用 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 制作计时器的最基本概念。而且会很有用。

【讨论】:

  • 我会在哪里合并 .after()
  • 取决于你想做什么......你发布的代码不能正常工作
  • 好的。我试图用发布的代码完成的事情是让它说在指定的时间点击我。但我需要提前让它说 10,9,8,7 ..... 在它说点击我之前。然后在 0 分钟的第 60 秒表示指定时间。
猜你喜欢
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多