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()
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. And it will be very useful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.