Skip to content

Commit 87c8e77

Browse files
Add timer support to kivy backend
1 parent 9a9a90a commit 87c8e77

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

backend_kivy.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ def close(event):
298298
from kivy.uix.textinput import TextInput
299299
from kivy.lang import Builder
300300
from kivy.logger import Logger
301+
from kivy.clock import Clock
301302
from distutils.version import LooseVersion
302303

303304
_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
@@ -1177,6 +1211,20 @@ def print_png(self, filename, *args, **kwargs):
11771211
def get_default_filetype(self):
11781212
return 'png'
11791213

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

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

0 commit comments

Comments
 (0)