forked from nhfruchter/music-transcription
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbettertimer.py
More file actions
42 lines (33 loc) · 1.01 KB
/
bettertimer.py
File metadata and controls
42 lines (33 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import time
from threading import Thread
class Timer(Thread):
# CITE: http://stackoverflow.com/questions/12435211/python-threading-timer-repeat-function-every-n-seconds
"""A (slightly) better timer for Tkinter. At the millisecond level,
Tk.after() is very jittery and this seems to work a lot better.
timer = Timer(delay_seconds, function_to_call)
Example:
from bettertimer import *
timer = Timer(0.001, self.timerFired )
timer.run()
"""
def __init__(self, delay, function):
self.stopped = False
self.paused = False
self.delay = delay # seconds
self.function = function
self.firedCounter = 0 # how many times the timer has fired
Thread.__init__(self)
def stop(self):
self.stopped = True
def pause(self):
self.paused = not self.paused
def run(self):
"""Implementation of threading.Thread() run function, which is the
thread's main loop."""
while not self.stopped:
if ( not self.paused ):
self.firedCounter += 1
self.function()
time.sleep(self.delay)
else:
continue