[Tutor] Code Regress
Evan Sommer
evanlespaul at gmail.com
Mon Feb 1 09:22:20 EST 2016
Hey again Alan!
Sorry it has been so long since I have been in contact.
I tried the code suggestion that you proposed in december, and while
it did count down time, it actually set me back in a way because the
display is not how I desired it to be.
The way the display looks in this program below is how I have been
asked to run it, and all that I need to change is the fact that the
program adds 2 separate windows when it changes color. I just need one
window. That is all I am trying to change. I realize that there may be
a different command to prevent that, which is what I am trying to
achieve, but I need the display settings to remain the same.
I'm not sure how to impose this display with the code you suggested
and make the program still run without errors popping up.
So if you know how to put the display on your suggested code in the
previous comment, then I would be much obliged, or if you can impose
your changes on to this code specifically I would be very grateful.
My engineering teacher isn't even sure how to do this, so you are my
only hope Alan!
Thank you for putting up with this madness!!
Evan
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='forest 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='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, -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='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, -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()
More information about the Tutor
mailing list