A Second Counter using Tkinter GUI

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
vegaseat vegaseat is offline Offline Aug 17th, 2007, 12:57 pm |
0
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.
Quick reply to this message  
Python Syntax
  1. # a second counter using Tkinter
  2. # tested with Python25 by vegaseat 17aug2007
  3.  
  4. import Tkinter as tk
  5. from itertools import count
  6.  
  7. def start_counter(label):
  8. counter = count(0)
  9. def update_func():
  10. label.config(text=str(counter.next()))
  11. label.after(1000, update_func) # 1000ms
  12. update_func()
  13.  
  14.  
  15. root = tk.Tk()
  16. root.title("Counting Seconds")
  17. label = tk.Label(root, fg="red")
  18. label.pack()
  19. start_counter(label)
  20. button = tk.Button(root, text='Stop', width=30, command=root.destroy)
  21. button.pack()
  22. root.mainloop()

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC