here2share

Tk_stopwatch.py

Mar 5th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. # Tk_stopwatch.py
  2.  
  3. from Tkinter import *
  4. import datetime
  5.  
  6. def update_timeText():
  7.     dtx = datetime.datetime.utcnow() - timeText.prev
  8.     timeText.prev = datetime.datetime.utcnow()
  9.     if state:
  10.         timeText.curr += dtx
  11.         dt = timeText.curr
  12.         s = dt.seconds
  13.         ms = dt.microseconds / 10000
  14.         m = s / 60
  15.        
  16.         timeString = pattern.format(m,s%60,ms)
  17.         if m > 99:
  18.             timeString = '99:99:99'
  19.                
  20.         if not timeText.onSplit:
  21.             timeText.configure(text=timeString, fg='green')
  22.            
  23.     root.after(1, update_timeText)
  24.  
  25. def start_x_pause():
  26.     global state
  27.     if timeText.onSplit:
  28.         timeText.onSplit = 0
  29.         update_timeText()
  30.     state = (1,0)[state]
  31.     if timeText.curr == zero:
  32.         timeText.prev = datetime.datetime.utcnow()
  33.     timeText.configure(fg=('red'))
  34.  
  35. def reset():
  36.     timeText.configure(text='00:00:00', fg='black')
  37.     timeText.prev = datetime.datetime.utcnow()
  38.     timeText.curr = zero
  39.     timeText.onSplit = 0
  40.  
  41. def split():
  42.     if state:
  43.         timeText.configure(fg='blue')
  44.         timeText.onSplit = (1,0)[timeText.onSplit]
  45.  
  46. def exist():
  47.     root.destroy()
  48.  
  49. state = 0
  50.  
  51. root = Tk()
  52. root.wm_title('Tkinter Stopwatch')
  53.  
  54. zero = datetime.datetime.now() - datetime.datetime.now()
  55.  
  56. pattern = '{0:02d}:{1:02d}:{2:02d}'
  57.  
  58. timeText = Label(root, font=("Helvetica", 150))
  59. timeText.pack()
  60.  
  61. start_x_pauseButton = Button(root, text='Start/Pause', command=start_x_pause)
  62. start_x_pauseButton.pack(side=LEFT)
  63.  
  64. splitButton = Button(root, text='Split', command=split)
  65. splitButton.pack(side=LEFT)
  66.  
  67. resetButton = Button(root, text='Reset', command=reset)
  68. resetButton.pack(side=LEFT)
  69.  
  70. quitButton = Button(root, text='Quit', command=exist)
  71. quitButton.pack(side=LEFT)
  72.  
  73. reset()
  74. update_timeText()
  75. root.mainloop()
Add Comment
Please, Sign In to add comment