dinoflex
Posts: 1
Joined: Sun Apr 21, 2019 1:57 am

Countdown timer

Sun Apr 21, 2019 2:33 am

Hello all. Being my first go around at any bit of programming work I am having a bit of a problem. I wanted to build a countdown timer for a trip that we are taking my son on. I found a design guide online and I could get it to work as described (https://www.instructables.com/id/Upcomi ... own-Timer/). As designed it displayed days, hours, and minutes. I added seconds on my own and I did get it to work. (I was quite proud of myself)

Here's the problem-After a short amount of time the seconds hang up and lag. It keeps correct time but the seconds start to jump 2-3 seconds at a time. One of my co-workers said that it could be an "over run issue". I'm not sure what that means?

If someone could take a look at the code and see what the issue could be I would be most appreciative.

Thank you very much.

#
#count down timer

Code: Select all

# by Brian Corteil aka on Twitter @CannonFodder
#
# free to use
#

import time, datetime
from datetime import timedelta
import Tkinter
from time import *

# enter Event target time and date here
day= 28
month= 06
year= 2019
hour= 8
minutes= 00
sec= 00
targetTime = datetime.datetime(year, month, day, hour, minutes) # sets up target time

def update():
    timeNow =datetime.datetime.now() 
    remainingTime=(targetTime-timeNow)
    days = remainingTime.days
    if days >= 30:
        canvas.create_rectangle(30,340,770,460,fill='hot pink')
    elif days < 30 and days >= 7:
        canvas.create_rectangle(30,340,770,460,fill='yellow')
    elif days < 7 and days >= 0:
        canvas.create_rectangle(30,340,770,460,fill='green')
    else:
        canvas.create_rectangle(30,340,770,460,fill='red')
                                
    canvas.create_text(100, 360, anchor='center',text='Days', font=('Ariel','20'))
    canvas.create_text(300, 360, anchor='center',text='Hours',font=('Ariel','20'))
    canvas.create_text(500, 360, anchor='center',text='Minutes',font=('Ariel','20'))
    canvas.create_text(700, 360, anchor='center',text='Seconds',font=('Ariel','20'))
                                
    canvas.create_text(100, 410, anchor='center',text=str(days),font=('Ariel','60'),fill='black')
    elapsed = remainingTime.seconds
    daysLeft = elapsed / (24 * 60 * 60)
    hoursLeft = (elapsed / (60 * 60)) % 24
    elapsed %= (60 * 60)
    minutesLeft = (elapsed / 60) % 60
    elapsed %= 60
    secsLeft = elapsed % 60
    
    canvas.create_text(300, 410, anchor='center',text=str(hoursLeft),font=('Ariel','60'),fill='black')
    canvas.create_text(500, 410, anchor='center',text=str(minutesLeft),font=('Ariel','60'),fill='black')
    canvas.create_text(700, 410, anchor='center',text=str(secsLeft),font=('Ariel','60'),fill='black')

    #secs = remainingTime.seconds
    
    #hrs, secs = divmod(secs, 3600)
    #canvas.create_text(300, 410, anchor='center',text=str(hrs),font=('Ariel','60'),fill='black')
    #mins, secs = divmod(secs, 60)
    #canvas.create_text(500, 410, anchor='center',text=str(mins),font=('Ariel','60'),fill='black')

    #canvas.create_text(700, 410, anchor='center',text=str(secs),font=('Ariel','60'),fill='black')
    root.after(1000,update)
    
                                
root=Tkinter.Tk()  
root.title('Upcoming Event Countdown Timer')
canvas = Tkinter.Canvas(root, height=480, width=800)
canvas.grid(row =10, column =3, sticky='w')
photo = Tkinter.PhotoImage(file = './IMG_2853.gif')
# resize image
photo = photo.zoom(1)
photo = photo.subsample(1)
canvas.create_image(0, 0, anchor='nw',image=photo)
canvas.create_rectangle(0,0,800,65,fill='blue')
# enter Event info here ...
canvas.create_text(400,20,anchor='center',text='Day Out With Thomas',font=('Helvetica','20'),fill='white')
canvas.create_text(400, 50, anchor='center',text='June 28th-30th', font=('Helvetica','20'),fill='yellow')

root.after(1000,update)
root.mainloop()
  
exit()

Return to “Other projects”