Saturday, June 05, 2010

[Py] Countdown counter

Here is a countdown counter which counts down in HH:MM:SS format. The original example has given by vegaseat, which counts increasingly. I modified it to be a countdown version as the follows.


"""
A ``countdown'' counter using Tkinter
Original version has given by vegaseat 17aug2007
http://www.daniweb.com/code/snippet216971.html

This version is modified from the original one
by thk 2010/06/05
"""

import Tkinter as tk
from itertools import count

def start_counter_down(label):
counter = count(0)
begin_time = 10 # sec.
def update_func():
left_time = begin_time - counter.next()
show_hr = left_time/3600
show_min = (left_time%3600)/60
show_sec = (left_time%3660)%60
label.config(text=\
str(show_hr).zfill(2)+':'+\
str(show_min).zfill(2)+':'+\
str(show_sec).zfill(2))
label.after(1000, update_func) # 1000ms
if left_time <= 0: label.config(text="Time's up!") update_func() root = tk.Tk() root.title("Counting Down") label = tk.Label(root, fg="red") label.pack() start_counter_down(label) button = tk.Button(root, text='Stop & Quit', width=30, command=root.destroy) button.pack() root.mainloop()

No comments:

Post a Comment