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

Skip to content

Commit c8e3742

Browse files
committed
Simplify canvas class control in FigureFrameWx.
Remember that the wx backends have a slightly different init order because the canvas cannot be instantiated *before* the containing window (a "frame" in wx parlance), so previously different wx backends implemented different frame subclasses so that each of them can instantiate different canvas subclasses. Instead of doing that (and having to assign them to the private `_frame_class` attribute, which is a problem e.g. for third-party backends such as mplcairo), directly pass the canvas class as a parameter to FigureFrameWx.
1 parent ec99c15 commit c8e3742

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
``FigureFrameWx`` constructor, subclasses, and ``get_canvas``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
The ``FigureCanvasWx`` constructor gained a *canvas_class* keyword-only
4+
parameter which specifies the canvas class that should be used. This
5+
parameter will become required in the future. The ``get_canvas`` method,
6+
which was previously used to customize canvas creation, is deprecated. The
7+
``FigureFrameWxAgg`` and ``FigureFrameWxCairo`` subclasses, which overrode
8+
``get_canvas``, are deprecated.

lib/matplotlib/backends/backend_wx.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ def _print_image(self, filetype, filename):
892892

893893

894894
class FigureFrameWx(wx.Frame):
895-
def __init__(self, num, fig):
895+
def __init__(self, num, fig, *, canvas_class=None):
896896
# On non-Windows platform, explicitly set the position - fix
897897
# positioning bug on some Linux platforms
898898
if wx.Platform == '__WXMSW__':
@@ -905,7 +905,15 @@ def __init__(self, num, fig):
905905
self.num = num
906906
_set_frame_icon(self)
907907

908-
self.canvas = self.get_canvas(fig)
908+
# The parameter will become required after the deprecation elapses.
909+
if canvas_class is not None:
910+
self.canvas = canvas_class(self, -1, fig)
911+
else:
912+
_api.warn_deprecated(
913+
"3.6", message="The canvas_class parameter will become "
914+
"required after the deprecation period starting in Matplotlib "
915+
"%(since)s elapses.")
916+
self.canvas = self.get_canvas(fig)
909917
w, h = map(math.ceil, fig.bbox.size)
910918
self.canvas.SetInitialSize(wx.Size(w, h))
911919
self.canvas.SetFocus()
@@ -953,6 +961,8 @@ def _get_toolbar(self):
953961
toolbar = None
954962
return toolbar
955963

964+
@_api.deprecated(
965+
"3.6", alternative="the canvas_class constructor parameter")
956966
def get_canvas(self, fig):
957967
return FigureCanvasWx(self, -1, fig)
958968

@@ -1393,7 +1403,6 @@ def trigger(self, *args, **kwargs):
13931403
class _BackendWx(_Backend):
13941404
FigureCanvas = FigureCanvasWx
13951405
FigureManager = FigureManagerWx
1396-
_frame_class = FigureFrameWx
13971406

13981407
@classmethod
13991408
def new_figure_manager(cls, num, *args, **kwargs):
@@ -1410,7 +1419,7 @@ def new_figure_manager(cls, num, *args, **kwargs):
14101419

14111420
@classmethod
14121421
def new_figure_manager_given_figure(cls, num, figure):
1413-
frame = cls._frame_class(num, figure)
1422+
frame = FigureFrameWx(num, figure, canvas_class=cls.FigureCanvas)
14141423
figmgr = frame.get_figure_manager()
14151424
if mpl.is_interactive():
14161425
figmgr.frame.Show()

lib/matplotlib/backends/backend_wxagg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import wx
22

3+
from .. import _api
34
from .backend_agg import FigureCanvasAgg
45
from .backend_wx import (
56
_BackendWx, _FigureCanvasWxBase, FigureFrameWx,
67
NavigationToolbar2Wx as NavigationToolbar2WxAgg)
78

89

10+
@_api.deprecated(
11+
"3.6", alternative="FigureFrameWx(..., canvas_class=FigureCanvasWxAgg)")
912
class FigureFrameWxAgg(FigureFrameWx):
1013
def get_canvas(self, fig):
1114
return FigureCanvasWxAgg(self, -1, fig)
@@ -56,4 +59,3 @@ def _rgba_to_wx_bitmap(rgba):
5659
@_BackendWx.export
5760
class _BackendWxAgg(_BackendWx):
5861
FigureCanvas = FigureCanvasWxAgg
59-
_frame_class = FigureFrameWxAgg

lib/matplotlib/backends/backend_wxcairo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import wx.lib.wxcairo as wxcairo
22

3+
from .. import _api
34
from .backend_cairo import cairo, FigureCanvasCairo
45
from .backend_wx import (
56
_BackendWx, _FigureCanvasWxBase, FigureFrameWx,
67
NavigationToolbar2Wx as NavigationToolbar2WxCairo)
78

89

10+
@_api.deprecated(
11+
"3.6", alternative="FigureFrameWx(..., canvas_class=FigureCanvasWxCairo)")
912
class FigureFrameWxCairo(FigureFrameWx):
1013
def get_canvas(self, fig):
1114
return FigureCanvasWxCairo(self, -1, fig)
@@ -36,4 +39,3 @@ def draw(self, drawDC=None):
3639
@_BackendWx.export
3740
class _BackendWxCairo(_BackendWx):
3841
FigureCanvas = FigureCanvasWxCairo
39-
_frame_class = FigureFrameWxCairo

0 commit comments

Comments
 (0)