A Second Counter using Tkinter GUI

vegaseat 1 Tallied Votes 3K Views Share

Using count() from Python's itertools module and the Tkinter GUI toolkit's after() function, you can create a simple counter that counts up the seconds until the Stop button is pressed.

# a second counter using Tkinter
# tested with Python25  by  vegaseat  17aug2007

import Tkinter as tk
from itertools import count

def start_counter(label):
    counter = count(0)
    def update_func():
        label.config(text=str(counter.next()))
        label.after(1000, update_func)  # 1000ms
    update_func()


root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="red")
label.pack()
start_counter(label)
button = tk.Button(root, text='Stop', width=30, command=root.destroy)
button.pack()
root.mainloop()