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

Skip to content

Commit af2bc5e

Browse files
authored
Merge pull request #16418 from anntzer/timercls
MNT: Backend timer simplifications.
2 parents b583623 + fce40b2 commit af2bc5e

File tree

11 files changed

+24
-39
lines changed

11 files changed

+24
-39
lines changed

doc/api/next_api_changes/removals.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ Arguments
122122
- The parameter ``block`` of ``show()`` is now keyword-only.
123123
- The parameter ``frameon`` of `.Figure.savefig` has been removed. Use
124124
``facecolor="none"`` to get a transparent background.
125+
- Passing a ``wx.EvtHandler`` as the first argument to ``backend_wx.TimerWx``
126+
is not supported anymore; the signature of ``TimerWx`` is now consistent with
127+
`.TimerBase`.
125128

126129
rcParams
127130
~~~~~~~~

examples/event_handling/pong_sgskip.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ def local_draw():
4444

4545

4646
start_anim.cid = canvas.mpl_connect('draw_event', start_anim)
47-
start_anim.timer = animation.canvas.new_timer()
48-
start_anim.timer.interval = 1
47+
start_anim.timer = animation.canvas.new_timer(interval=1)
4948

5049
tstart = time.time()
5150

examples/user_interfaces/embedding_in_qt_sgskip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def __init__(self):
4545
self._static_ax.plot(t, np.tan(t), ".")
4646

4747
self._dynamic_ax = dynamic_canvas.figure.subplots()
48-
self._timer = dynamic_canvas.new_timer(
49-
50, [(self._update_canvas, (), {})])
48+
self._timer = dynamic_canvas.new_timer(50)
49+
self._timer.add_callback(self._update_canvas)
5050
self._timer.start()
5151

5252
def _update_canvas(self):

lib/matplotlib/animation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,8 +1401,7 @@ def __init__(self, fig, interval=200, repeat_delay=0, repeat=True,
14011401
# If we're not given an event source, create a new timer. This permits
14021402
# sharing timers between animation objects for syncing animations.
14031403
if event_source is None:
1404-
event_source = fig.canvas.new_timer()
1405-
event_source.interval = self._interval
1404+
event_source = fig.canvas.new_timer(interval=self._interval)
14061405
Animation.__init__(self, fig, event_source=event_source,
14071406
*args, **kwargs)
14081407

lib/matplotlib/backend_bases.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@ class itself will store the flag and the ``_on_timer`` method should be
10571057
timer events. This list can be manipulated directly, or the
10581058
functions `add_callback` and `remove_callback` can be used.
10591059
"""
1060+
10601061
def __init__(self, interval=None, callbacks=None):
10611062
#Initialize empty callbacks list and setup default settings if necssary
10621063
if callbacks is None:
@@ -2222,15 +2223,19 @@ def mpl_disconnect(self, cid):
22222223
"""
22232224
return self.callbacks.disconnect(cid)
22242225

2225-
def new_timer(self, *args, **kwargs):
2226+
# Internal subclasses can override _timer_cls instead of new_timer, though
2227+
# this is not a public API for third-party subclasses.
2228+
_timer_cls = TimerBase
2229+
2230+
def new_timer(self, interval=None, callbacks=None):
22262231
"""
22272232
Create a new backend-specific subclass of `.Timer`.
22282233
22292234
This is useful for getting periodic events through the backend's native
22302235
event loop. Implemented only for backends with GUIs.
22312236
2232-
Other Parameters
2233-
----------------
2237+
Parameters
2238+
----------
22342239
interval : int
22352240
Timer interval in milliseconds.
22362241
@@ -2243,9 +2248,9 @@ def new_timer(self, *args, **kwargs):
22432248
22442249
Examples
22452250
--------
2246-
>>> timer = fig.canvas.new_timer(callbacks=[(f1, (1, ), {'a': 3}),])
2251+
>>> timer = fig.canvas.new_timer(callbacks=[(f1, (1,), {'a': 3})])
22472252
"""
2248-
return TimerBase(*args, **kwargs)
2253+
return self._timer_cls(interval=interval, callbacks=callbacks)
22492254

22502255
def flush_events(self):
22512256
"""

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def _on_timer(self):
9898

9999
class FigureCanvasGTK3(Gtk.DrawingArea, FigureCanvasBase):
100100
required_interactive_framework = "gtk3"
101+
_timer_cls = TimerGTK3
101102

102103
keyvald = {65507: 'control',
103104
65505: 'shift',
@@ -305,10 +306,6 @@ def idle_draw(*args):
305306
return False
306307
self._idle_draw_id = GLib.idle_add(idle_draw)
307308

308-
def new_timer(self, *args, **kwargs):
309-
# docstring inherited
310-
return TimerGTK3(*args, **kwargs)
311-
312309
def flush_events(self):
313310
# docstring inherited
314311
Gdk.threads_enter()

lib/matplotlib/backends/backend_macosx.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class FigureCanvasMac(_macosx.FigureCanvas, FigureCanvasAgg):
5555
"""
5656

5757
required_interactive_framework = "macosx"
58+
_timer_cls = TimerMac
5859

5960
def __init__(self, figure):
6061
FigureCanvasBase.__init__(self, figure)
@@ -99,10 +100,6 @@ def resize(self, width, height):
99100
FigureCanvasBase.resize_event(self)
100101
self.draw_idle()
101102

102-
def new_timer(self, *args, **kwargs):
103-
# docstring inherited
104-
return TimerMac(*args, **kwargs)
105-
106103

107104
class FigureManagerMac(_macosx.FigureManager, FigureManagerBase):
108105
"""

lib/matplotlib/backends/backend_nbagg.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ def remove_comm(self, comm_id):
142142

143143

144144
class FigureCanvasNbAgg(FigureCanvasWebAggCore):
145-
def new_timer(self, *args, **kwargs):
146-
# docstring inherited
147-
return TimerTornado(*args, **kwargs)
145+
_timer_cls = TimerTornado
148146

149147

150148
class CommSocket:

lib/matplotlib/backends/backend_qt5.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def _timer_stop(self):
215215

216216
class FigureCanvasQT(QtWidgets.QWidget, FigureCanvasBase):
217217
required_interactive_framework = "qt5"
218+
_timer_cls = TimerQT
218219

219220
# map Qt button codes to MouseEvent's ones:
220221
buttond = {QtCore.Qt.LeftButton: MouseButton.LEFT,
@@ -434,10 +435,6 @@ def _get_key(self, event):
434435
mods.reverse()
435436
return '+'.join(mods + [key])
436437

437-
def new_timer(self, *args, **kwargs):
438-
# docstring inherited
439-
return TimerQT(*args, **kwargs)
440-
441438
def flush_events(self):
442439
# docstring inherited
443440
qApp.processEvents()

lib/matplotlib/backends/backend_webagg.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ def run(self):
4848

4949

5050
class FigureCanvasWebAgg(core.FigureCanvasWebAggCore):
51+
_timer_cls = TimerTornado
52+
5153
def show(self):
5254
# show the figure window
5355
global show # placates pyflakes: created by @_Backend.export below
5456
show()
5557

56-
def new_timer(self, *args, **kwargs):
57-
# docstring inherited
58-
return TimerTornado(*args, **kwargs)
59-
6058

6159
class WebAggApplication(tornado.web.Application):
6260
initialized = False

lib/matplotlib/backends/backend_wx.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ class TimerWx(TimerBase):
8686
"""
8787

8888
def __init__(self, *args, **kwargs):
89-
if args and isinstance(args[0], wx.EvtHandler):
90-
cbook.warn_deprecated(
91-
"3.0", message="Passing a wx.EvtHandler as first argument to "
92-
"the TimerWx constructor is deprecated since %(since)s.")
93-
args = args[1:]
9489
TimerBase.__init__(self, *args, **kwargs)
9590
self._timer = wx.Timer()
9691
self._timer.Notify = self._on_timer
@@ -468,6 +463,7 @@ class _FigureCanvasWxBase(FigureCanvasBase, wx.Panel):
468463
"""
469464

470465
required_interactive_framework = "wx"
466+
_timer_cls = TimerWx
471467

472468
keyvald = {
473469
wx.WXK_CONTROL: 'control',
@@ -598,10 +594,6 @@ def draw_idle(self):
598594
# a good time (usually as soon as there are no other events pending).
599595
self.Refresh(eraseBackground=False)
600596

601-
def new_timer(self, *args, **kwargs):
602-
# docstring inherited
603-
return TimerWx(*args, **kwargs)
604-
605597
def flush_events(self):
606598
# docstring inherited
607599
wx.Yield()

0 commit comments

Comments
 (0)