Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_stopwatch.py
- from Tkinter import *
- import datetime
- def update_timeText():
- dtx = datetime.datetime.utcnow() - timeText.prev
- timeText.prev = datetime.datetime.utcnow()
- if state:
- timeText.curr += dtx
- dt = timeText.curr
- s = dt.seconds
- ms = dt.microseconds / 10000
- m = s / 60
- timeString = pattern.format(m,s%60,ms)
- if m > 99:
- timeString = '99:99:99'
- if not timeText.onSplit:
- timeText.configure(text=timeString, fg='green')
- root.after(1, update_timeText)
- def start_x_pause():
- global state
- if timeText.onSplit:
- timeText.onSplit = 0
- update_timeText()
- state = (1,0)[state]
- if timeText.curr == zero:
- timeText.prev = datetime.datetime.utcnow()
- timeText.configure(fg=('red'))
- def reset():
- timeText.configure(text='00:00:00', fg='black')
- timeText.prev = datetime.datetime.utcnow()
- timeText.curr = zero
- timeText.onSplit = 0
- def split():
- if state:
- timeText.configure(fg='blue')
- timeText.onSplit = (1,0)[timeText.onSplit]
- def exist():
- root.destroy()
- state = 0
- root = Tk()
- root.wm_title('Tkinter Stopwatch')
- zero = datetime.datetime.now() - datetime.datetime.now()
- pattern = '{0:02d}:{1:02d}:{2:02d}'
- timeText = Label(root, font=("Helvetica", 150))
- timeText.pack()
- start_x_pauseButton = Button(root, text='Start/Pause', command=start_x_pause)
- start_x_pauseButton.pack(side=LEFT)
- splitButton = Button(root, text='Split', command=split)
- splitButton.pack(side=LEFT)
- resetButton = Button(root, text='Reset', command=reset)
- resetButton.pack(side=LEFT)
- quitButton = Button(root, text='Quit', command=exist)
- quitButton.pack(side=LEFT)
- reset()
- update_timeText()
- root.mainloop()
Add Comment
Please, Sign In to add comment