0

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

Kg123
  • 107
  • 1
  • 1
  • 11

2 Answers2

0

Shouldn't you do?

secs = delta_t.total_seconds() + 1

Instead of

secs = delta_t.seconds + 1
DevLounge
  • 8,313
  • 3
  • 31
  • 44
  • Thanks for the suggestion. It seems to make it work more efficiently but how do I make it countdown to that time – Kg123 Mar 21 '17 at 21:25
0

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.

Taku
  • 31,927
  • 11
  • 74
  • 85
  • Where would I incorporate the .after() – Kg123 Mar 21 '17 at 23:43
  • Depends on what you want to do...you posted code doesn't work well – Taku Mar 22 '17 at 04:48
  • Ok. The thing I was trying to accomplish with the posted code was to make it say click me at the specified time. But I need to advance it and make it say 10,9,8,7..... before it says click me. Then at 0 which is the 60th second of the minute it says the specified time. – Kg123 Mar 22 '17 at 11:20