Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e549e9

Browse files
committed
Merge pull request kivy-garden#32 from jonatanolofsson/timer
Add timer support to kivy backend
2 parents 6cc474a + 87c8e77 commit 0e549e9

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

backend_kivy.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ def close(event):
296296
from kivy.uix.textinput import TextInput
297297
from kivy.lang import Builder
298298
from kivy.logger import Logger
299+
from kivy.clock import Clock
299300
from distutils.version import LooseVersion
300301

301302
_mpl_1_5 = LooseVersion(matplotlib.__version__) >= LooseVersion('1.5.0')
@@ -995,6 +996,39 @@ def _get_style_dict(self, rgbFace):
995996
return attrib
996997

997998

999+
class TimerKivy(TimerBase):
1000+
'''
1001+
Subclass of :class:`backend_bases.TimerBase` that uses Kivy for timer events.
1002+
Attributes:
1003+
* interval: The time between timer events in milliseconds. Default
1004+
is 1000 ms.
1005+
* single_shot: Boolean flag indicating whether this timer should
1006+
operate as single shot (run once and then stop). Defaults to False.
1007+
* callbacks: Stores list of (func, args) tuples that will be called
1008+
upon timer events. This list can be manipulated directly, or the
1009+
functions add_callback and remove_callback can be used.
1010+
'''
1011+
def _timer_start(self):
1012+
# Need to stop it, otherwise we potentially leak a timer id that will
1013+
# never be stopped.
1014+
self._timer_stop()
1015+
self._timer = Clock.schedule_interval(self._on_timer, self._interval / 1000.0)
1016+
1017+
def _timer_stop(self):
1018+
if self._timer is not None:
1019+
Clock.unschedule(self._timer)
1020+
self._timer = None
1021+
1022+
def _timer_set_interval(self):
1023+
# Only stop and restart it if the timer has already been started
1024+
if self._timer is not None:
1025+
self._timer_stop()
1026+
self._timer_start()
1027+
1028+
def _on_timer(self, dt):
1029+
super(TimerKivy, self)._on_timer()
1030+
1031+
9981032
class FigureCanvasKivy(FocusBehavior, Widget, FigureCanvasBase):
9991033
'''FigureCanvasKivy class. See module documentation for more information.
10001034
'''
@@ -1175,6 +1209,20 @@ def print_png(self, filename, *args, **kwargs):
11751209
def get_default_filetype(self):
11761210
return 'png'
11771211

1212+
def new_timer(self, *args, **kwargs):
1213+
"""
1214+
Creates a new backend-specific subclass of :class:`backend_bases.Timer`.
1215+
This is useful for getting periodic events through the backend's native
1216+
event loop. Implemented only for backends with GUIs.
1217+
optional arguments:
1218+
*interval*
1219+
Timer interval in milliseconds
1220+
*callbacks*
1221+
Sequence of (func, args, kwargs) where func(*args, **kwargs) will
1222+
be executed by the timer every *interval*.
1223+
"""
1224+
return TimerKivy(*args, **kwargs)
1225+
11781226

11791227
class FigureManagerKivy(FigureManagerBase):
11801228
'''The FigureManager main function is to instantiate the backend navigation

0 commit comments

Comments
 (0)