[Tutor] Countdown Clock Programming Question

Evan Sommer evanlespaul at gmail.com
Tue Dec 15 09:18:47 EST 2015


Hey there Alan!

So I tried some of your modifications, and they seem to be doing some
pretty interesting things with the program. For some reason, now the
program runs extremely fast, and doesn't now count down in one second
intervals. I think this is a result of the root.after command. I still
get 3 different windows with 3 different colours as well.

I'm probably just doing something completely wrong, so if you could
show me where to edit the code, I would much appreciate it!

Thanks!

Here is the most recent code:

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
import time
def count_down(start, end):
        root.after (1000, count_down)
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='forestgreen',
         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, -15):
        # 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
        root.after (1, count_down)
# 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='gold',
         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, -15):
        # 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
        root.after (1, count_down)
# 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='firebrick',
         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, -15):
        # 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
        root.after (1, count_down)
# start the GUI event loop
root.mainloop()


On 11/30/15, Evan Sommer <evanlespaul at gmail.com> 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.
>
> If you could do that, I would greatly appreciate it!!
>
> Thank you for all your help!
>
> Evan Sommer
>
> On Tue, Nov 24, 2015 at 9:01 AM, Evan Sommer <evanlespaul at gmail.com> wrote:
>
>> Hello there!
>>
>> I am working on a project for an engineering class that I am in at my
>> high
>> school, and our task has been to create a clock that counts down the time
>> between periods, so that the students walking the hallways know how much
>> time they have to get to class. The timer will be displayed on multiple
>> monitors throughout the halls. However, the idea behind the countdown
>> clock
>> is for the background to change colours when it hits a certain time. The
>> goal is for the clock to change from green to yellow at 2 minutes, and
>> yellow to red at 1 minute. However, I have been having a hard time trying
>> to get the color change to display in one window. If you could give me
>> some
>> advice, I'd really appreciate it!
>>
>> Here's the code:
>>
>> try:
>>     # Python2
>>     import Tkinter as tk
>> except ImportError:
>>     # Python3
>>     import tkinter as tk
>> 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
>> 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):
>>         # 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):
>>         # 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):
>>         # 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()
>>
>> Thanks for the help!
>>
>> Sincerely,
>>
>> Evan Sommer
>>
>


More information about the Tutor mailing list