Advertisement
Dori_mon

Timer Python

Jul 12th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. import threading
  2. import time
  3.  
  4. class Timer:
  5.  
  6.     def __init__(self, **kwargs):
  7.         self._time = kwargs.get('starting_time', 0)
  8.  
  9.     def _count(self):
  10.         while True:
  11.             self._time += 1
  12.             time.sleep(1)
  13.  
  14.     def start(self):
  15.         t = threading.Thread(target=self._count)
  16.         t.start()
  17.         return t
  18.  
  19.     @property
  20.     def current(self):
  21.         return self._time
  22.  
  23.  
  24. # Create timer that starts counting from 0
  25. timer = Timer()
  26.  
  27. #Create timer that starts counting from any input number
  28. timer = Timer(5)
  29.  
  30. # Start the timer counting
  31. timer.start()
  32.  
  33. # Get current timer time
  34. timer.current
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement