[Tutor] Countdown Clock Programming Question

Alan Gauld alan.gauld at btinternet.com
Tue Dec 1 05:16:50 EST 2015


On 30/11/15 19:23, Evan Sommer wrote:
> Hello again Alan!
> 
>  Do you think you could write a revised code with the modifications that
> you suggested? I tried changing the code with your recommendations and I
> keep getting syntax errors.

My code was just an approximation known as 'pseudo code' you
need to translate it into real Python.

>> import time
>> def count_down():
>>     # start with 4 minutes --> 240 seconds
>>     for t in range(240, 120, -1):
>>         # format as 2 digit integers, fills with zero to the left
>>         # divmod() gives minutes, seconds
>>         sf = "{:01d}:{:02d}".format(*divmod(t, 60))
>>         #print(sf)  # test
>>         time_str.set(sf)
>>         root.update()
>>         # delay one second
>>         time.sleep(1)# create root/main window

Don't use time.sleep() in a Tkinter program use the after
method instead. In this case it will look like

root.after(1000, count_down)

Also instead of hard coding the 240 and 120 make them
parameters of the function like

def count_down(start, end):

and put those names into your loop:

for t in range(start,stop,-1)

That way you can call it multiple times with the
different values you need and avoid duplicating
the for loop below

Try those fixes initially and see how you go.

>> root = tk.Tk()
>> time_str = tk.StringVar()
>> # create the time display label, give it a large font
>> # label auto-adjusts to the font
>> label_font = ('helvetica', 100)
>> tk.Label(root, textvariable=time_str, font=label_font, bg='green',
>>          fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5)
>>  # start with 2 minutes --> 119 seconds
>> for t in range(240, 120, -1):

call count_down(240,120) here

>>         # format as 2 digit integers, fills with zero to the left
>>         # divmod() gives minutes, seconds
>>         sf = "{:01d}:{:02d}".format(*divmod(t, 60))
>>         #print(sf)  # test
>>         time_str.set(sf)
>>         root.update()
>>         # delay one second
>>         time.sleep(1)
>> # create the time display label, give it a large font
>> # label auto-adjusts to the font
>> label_font = ('helvetica', 100)
>> tk.Label(root, textvariable=time_str, font=label_font, bg='yellow',
>>          fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5)
>>  # start with 1 minutes --> 59 seconds
>> for t in range(120,60, -1):

call count_down(120,60) here

>>         # format as 2 digit integers, fills with zero to the left
>>         # divmod() gives minutes, seconds
>>         sf = "{:01d}:{:02d}".format(*divmod(t, 60))
>>         #print(sf)  # test
>>         time_str.set(sf)
>>         root.update()
>>         # delay one second
>>         time.sleep(1)
>> # create the time display label, give it a large font
>> # label auto-adjusts to the font
>> label_font = ('helvetica', 100)
>> tk.Label(root, textvariable=time_str, font=label_font, bg='red',
>>          fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5)
>>  # start with 4 minutes --> 240 seconds
>> for t in range(60,-1, -1):

call count_down(60,-1) here

>>         # format as 2 digit integers, fills with zero to the left
>>         # divmod() gives minutes, seconds
>>         sf = "{:01d}:{:02d}".format(*divmod(t, 60))
>>         #print(sf)  # test
>>         time_str.set(sf)
>>         root.update()
>>         # delay one second
>>         time.sleep(1)
>> # start the GUI event loop
>> root.mainloop()
>>

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list