|
| 1 | + |
| 2 | +import tkinter as Tkinter |
| 3 | +from datetime import datetime |
| 4 | +counter = 66600 |
| 5 | +running = False |
| 6 | +def counter_label(label): |
| 7 | + def count(): |
| 8 | + if running: |
| 9 | + global counter |
| 10 | + |
| 11 | + |
| 12 | + if counter==66600: |
| 13 | + display="Starting..." |
| 14 | + else: |
| 15 | + tt = datetime.fromtimestamp(counter) |
| 16 | + string = tt.strftime("%H:%M:%S") |
| 17 | + display=string |
| 18 | + |
| 19 | + label['text']=display # Or label.config(text=display) |
| 20 | + |
| 21 | + |
| 22 | + label.after(1000, count) |
| 23 | + counter += 1 |
| 24 | + |
| 25 | + |
| 26 | + count() |
| 27 | + |
| 28 | + |
| 29 | +def Start(label): |
| 30 | + global running |
| 31 | + running=True |
| 32 | + counter_label(label) |
| 33 | + start['state']='disabled' |
| 34 | + stop['state']='normal' |
| 35 | + reset['state']='normal' |
| 36 | + |
| 37 | +def Stop(): |
| 38 | + global running |
| 39 | + start['state']='normal' |
| 40 | + stop['state']='disabled' |
| 41 | + reset['state']='normal' |
| 42 | + running = False |
| 43 | + |
| 44 | + |
| 45 | +def Reset(label): |
| 46 | + global counter |
| 47 | + counter=66600 |
| 48 | + |
| 49 | + |
| 50 | + if running==False: |
| 51 | + reset['state']='disabled' |
| 52 | + label['text']='Welcome!' |
| 53 | + |
| 54 | + |
| 55 | + else: |
| 56 | + label['text']='Starting...' |
| 57 | + |
| 58 | +root = Tkinter.Tk() |
| 59 | +root.title("Stopwatch") |
| 60 | + |
| 61 | + |
| 62 | +root.minsize(width=250, height=70) |
| 63 | +label = Tkinter.Label(root, text="Welcome!", fg="black", font="Verdana 30 bold") |
| 64 | +label.pack() |
| 65 | +f = Tkinter.Frame(root) |
| 66 | +start = Tkinter.Button(f, text='Start', width=6, command=lambda:Start(label)) |
| 67 | +stop = Tkinter.Button(f, text='Stop',width=6,state='disabled', command=Stop) |
| 68 | +reset = Tkinter.Button(f, text='Reset',width=6, state='disabled', command=lambda:Reset(label)) |
| 69 | +f.pack(anchor = 'center',pady=5) |
| 70 | +start.pack(side="left") |
| 71 | +stop.pack(side ="left") |
| 72 | +reset.pack(side="left") |
| 73 | +root.mainloop() |
| 74 | + |
| 75 | + |
| 76 | + |
0 commit comments