[Tutor] Countdown Clock Programming Question

Evan Sommer evanlespaul at gmail.com
Tue Nov 24 09:01:58 EST 2015


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