Skip to content

Commit 7b14917

Browse files
authored
Merge pull request #17160 from anntzer/timerintinterval
Correctly go through property setter when init'ing Timer interval.
2 parents d971c59 + 96690e1 commit 7b14917

File tree

8 files changed

+22
-12
lines changed

8 files changed

+22
-12
lines changed

doc/api/api_changes_3.3/behaviour.rst

+5
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,8 @@ explicitly pass a "%1.2f" as the *valfmt* parameter to `.Slider`.
176176
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
177177
All the functionality of ``CustomCell`` has been moved to its base class
178178
`~.table.Cell`.
179+
180+
wx Timer interval
181+
~~~~~~~~~~~~~~~~~
182+
Setting the timer interval on a not-yet-started ``TimerWx`` won't start it
183+
anymore.

lib/matplotlib/backend_bases.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1059,10 +1059,9 @@ def __init__(self, interval=None, callbacks=None):
10591059
`remove_callback` can be used.
10601060
"""
10611061
self.callbacks = [] if callbacks is None else callbacks.copy()
1062-
self._interval = 1000 if interval is None else interval
1063-
self._single = False
1064-
# Default attribute for holding the GUI-specific timer object
1065-
self._timer = None
1062+
# Set .interval and not ._interval to go through the property setter.
1063+
self.interval = 1000 if interval is None else interval
1064+
self.single_shot = False
10661065

10671066
def __del__(self):
10681067
"""Need to stop timer and possibly disconnect timer."""

lib/matplotlib/backends/_backend_tk.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ class TimerTk(TimerBase):
8282
"""Subclass of `backend_bases.TimerBase` using Tk timer events."""
8383

8484
def __init__(self, parent, *args, **kwargs):
85+
self._timer = None
8586
TimerBase.__init__(self, *args, **kwargs)
8687
self.parent = parent
87-
self._timer = None
8888

8989
def _timer_start(self):
9090
self._timer_stop()
@@ -97,7 +97,6 @@ def _timer_stop(self):
9797

9898
def _on_timer(self):
9999
TimerBase._on_timer(self)
100-
101100
# Tk after() is only a single shot, so we need to add code here to
102101
# reset the timer if we're not operating in single shot mode. However,
103102
# if _timer is None, this means that _timer_stop has been called; so

lib/matplotlib/backends/backend_gtk3.py

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
class TimerGTK3(TimerBase):
5353
"""Subclass of `.TimerBase` using GTK3 timer events."""
5454

55+
def __init__(self, *args, **kwargs):
56+
self._timer = None
57+
TimerBase.__init__(self, *args, **kwargs)
58+
5559
def _timer_start(self):
5660
# Need to stop it, otherwise we potentially leak a timer id that will
5761
# never be stopped.

lib/matplotlib/backends/backend_qt5.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,11 @@ class TimerQT(TimerBase):
180180
"""Subclass of `.TimerBase` using QTimer events."""
181181

182182
def __init__(self, *args, **kwargs):
183-
TimerBase.__init__(self, *args, **kwargs)
184183
# Create a new timer and connect the timeout() signal to the
185184
# _on_timer method.
186185
self._timer = QtCore.QTimer()
187186
self._timer.timeout.connect(self._on_timer)
188-
self._timer_set_interval()
187+
TimerBase.__init__(self, *args, **kwargs)
189188

190189
def __del__(self):
191190
# The check for deletedness is needed to avoid an error at animation

lib/matplotlib/backends/backend_webagg_core.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ def _send_event(self, event_type, **kwargs):
489489

490490

491491
class TimerTornado(backend_bases.TimerBase):
492+
def __init__(self, *args, **kwargs):
493+
self._timer = None
494+
super().__init__(*args, **kwargs)
495+
492496
def _timer_start(self):
493497
self._timer_stop()
494498
if self._single:
@@ -510,7 +514,6 @@ def _timer_stop(self):
510514
ioloop.remove_timeout(self._timer)
511515
else:
512516
self._timer.stop()
513-
514517
self._timer = None
515518

516519
def _timer_set_interval(self):

lib/matplotlib/backends/backend_wx.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ class TimerWx(TimerBase):
7070
"""Subclass of `.TimerBase` using wx.Timer events."""
7171

7272
def __init__(self, *args, **kwargs):
73-
TimerBase.__init__(self, *args, **kwargs)
7473
self._timer = wx.Timer()
7574
self._timer.Notify = self._on_timer
75+
TimerBase.__init__(self, *args, **kwargs)
7676

7777
def _timer_start(self):
7878
self._timer.Start(self._interval, self._single)
@@ -81,7 +81,8 @@ def _timer_stop(self):
8181
self._timer.Stop()
8282

8383
def _timer_set_interval(self):
84-
self._timer_start()
84+
if self._timer.IsRunning():
85+
self._timer_start() # Restart with new interval.
8586

8687
def _timer_set_single_shot(self):
8788
self._timer.Start()

lib/matplotlib/tests/test_backends_interactive.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def check_alt_backend(alt_backend):
114114
115115
ax.plot([0, 1], [2, 3])
116116
117-
timer = fig.canvas.new_timer(1)
117+
timer = fig.canvas.new_timer(1.) # Test that floats are cast to int as needed.
118118
timer.add_callback(FigureCanvasBase.key_press_event, fig.canvas, "q")
119119
# Trigger quitting upon draw.
120120
fig.canvas.mpl_connect("draw_event", lambda event: timer.start())

0 commit comments

Comments
 (0)