8000 Correctly go through property setter when init'ing Timer interval. · matplotlib/matplotlib@8ffaf34 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ffaf34

Browse files
committed
Correctly go through property setter when init'ing Timer interval.
The setter casts the interval to an int, which is required at least by the tk backend. This caught a bug in the wx timer, where just setting the interval would start the timer. Fixed that.
1 parent 53faed9 commit 8ffaf34

File tree

7 files changed

+18
-11
lines changed

7 files changed

+18
-11
lines changed

doc/api/api_changes_3.3/behaviour.rst

Lines changed: 5 additions & 0 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 1 addition & 2 deletions
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_wx.py

Lines changed: 3 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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)
0