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

Skip to content

Commit 38927cd

Browse files
committed
Make FigureCanvasWx ctor signature similar to FigureCanvasBase.
Currently, `FigureCanvasWx` has a signature of `(parent, id, figure)`, unlike the Canvas classes of other backends, which only take `figure` as argument. Change `FigureCanvasWx` to be more similar to other backends. A similar fix will later be applied to `FigureManagerWx` to bring it in line with other Manager classes. The ultimate goal is to get rid of `new_figure_manager` and `new_figure_manager_given_figure` in the backend definitions, which can be replaced by `return FigureManager(FigureCanvas(Figure(...)), num)` and `return FigureManager(FigureCanvas(fig, num))` respectively. Also deprecate the aliases `FigureCanvasWx.frame` and `.tb` (which are named `.window` and `.toolbar` in all other backends).
1 parent e188fae commit 38927cd

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

doc/api/next_api_changes/2018-02-15-AL-deprecations.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ The following functions and classes are deprecated:
55
- ``cbook.GetRealpathAndStat`` (which is only a helper for
66
``get_realpath_and_stat``),
77
- ``cbook.is_numlike`` (use ``isinstance(..., numbers.Number)`` instead),
8+
- ``FigureCanvasWx.frame`` and ``.tb`` (use ``.window`` and ``.toolbar``
9+
respectively, which is consistent with other backends),
10+
- the ``FigureCanvasWx`` constructor should not be called with ``(parent, id,
11+
figure)`` as arguments anymore, but just ``figure`` (like all other canvas
12+
classes). Call ``Reparent`` and ``SetId`` to set the parent and id of the
13+
canvas.
814
- ``mathtext.unichr_safe`` (use ``chr`` instead),

lib/matplotlib/backends/backend_wx.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -613,16 +613,29 @@ class _FigureCanvasWxBase(FigureCanvasBase, wx.Panel):
613613
wx.WXK_NUMPAD_DELETE: 'delete',
614614
}
615615

616-
def __init__(self, parent, id, figure):
616+
def __init__(self, *args):
617617
"""
618-
Initialise a FigureWx instance.
618+
Initialize a FigureWx instance.
619619
620-
- Initialise the FigureCanvasBase and wxPanel parents.
621-
- Set event handlers for:
622-
EVT_SIZE (Resize event)
623-
EVT_PAINT (Paint event)
620+
Initialize the FigureCanvasBase and wxPanel parents and set event
621+
handlers.
622+
623+
Note that the canvas parent is set to the top window
624+
(``wx.App().GetTopWindow()``) and it is the responsibility of the
625+
caller to ``Reparent`` the canvas.
624626
"""
625627

628+
if len(args) == 3:
629+
# Legacy signature.
630+
cbook.warn_deprecated(
631+
"3.0", "Passing a parent and id to the FigureCanvasWx "
632+
"constructor is deprecated; use 'SetId' and 'Reparent' if "
633+
"needed.")
634+
parent, id, figure = args
635+
else:
636+
parent = wx.GetApp().GetTopWindow()
637+
id = wx.ID_ANY
638+
figure, = args
626639
FigureCanvasBase.__init__(self, figure)
627640
# Set preferred window size hint - helps the sizer (if one is
628641
# connected)
@@ -1227,7 +1240,9 @@ def _get_toolbar(self, statbar):
12271240
return toolbar
12281241

12291242
def get_canvas(self, fig):
1230-
return FigureCanvasWx(self, -1, fig)
1243+
canvas = FigureCanvasWx(fig)
1244+
canvas.Reparent(self)
1245+
return canvas
12311246

12321247
def get_figure_manager(self):
12331248
DEBUG_MSG("get_figure_manager()", 1, self)
@@ -1282,15 +1297,24 @@ def __init__(self, canvas, num, frame):
12821297
self.frame = frame
12831298
self.window = frame
12841299

1285-
self.tb = frame.GetToolBar()
1286-
self.toolbar = self.tb # consistent with other backends
1300+
self.toolbar = frame.GetToolBar()
12871301

12881302
def notify_axes_change(fig):
12891303
'this will be called whenever the current axes is changed'
1290-
if self.tb is not None:
1291-
self.tb.update()
1304+
if self.toolbar is not None:
1305+
self.toolbar.update()
12921306
self.canvas.figure.add_axobserver(notify_axes_change)
12931307

1308+
@property
1309+
@cbook.deprecated("3.0")
1310+
def frame(self):
1311+
return self.window
1312+
1313+
@property
1314+
@cbook.deprecated("3.0")
1315+
def tb(self):
1316+
return self.toolbar
1317+
12941318
def show(self):
12951319
self.frame.Show()
12961320
self.canvas.draw()
@@ -1477,7 +1501,8 @@ def __init__(self, targetfig):
14771501
wx.Frame.__init__(self, None, -1, "Configure subplots")
14781502

14791503
toolfig = Figure((6, 3))
1480-
canvas = FigureCanvasWx(self, -1, toolfig)
1504+
canvas = FigureCanvasWx(toolfig)
1505+
canvas.Reparent(self)
14811506

14821507
# Create a figure manager to manage things
14831508
figmgr = FigureManager(canvas, 1, self)
@@ -1493,7 +1518,7 @@ def __init__(self, targetfig):
14931518

14941519
class NavigationToolbar2Wx(NavigationToolbar2, wx.ToolBar):
14951520
def __init__(self, canvas):
1496-
wx.ToolBar.__init__(self, canvas.GetParent(), -1)
1521+
wx.ToolBar.__init__(self, canvas.GetParent())
14971522
NavigationToolbar2.__init__(self, canvas)
14981523
self.canvas = canvas
14991524
self._idle = True
@@ -1506,7 +1531,9 @@ def __init__(self, canvas):
15061531
self.retinaFix = 'wxMac' in wx.PlatformInfo
15071532

15081533
def get_canvas(self, frame, fig):
1509-
return type(self.canvas)(frame, -1, fig)
1534+
canvas = type(self.canvas)(fig)
1535+
canvas.Reparent(frame)
1536+
return canvas
15101537

15111538
def _init_toolbar(self):
15121539
DEBUG_MSG("_init_toolbar", 1, self)
@@ -1537,7 +1564,7 @@ def pan(self, *args):
15371564
NavigationToolbar2.pan(self, *args)
15381565

15391566
def configure_subplots(self, evt):
1540-
frame = wx.Frame(None, -1, "Configure subplots")
1567+
frame = wx.Frame(None, title="Configure subplots")
15411568

15421569
toolfig = Figure((6, 3))
15431570
canvas = self.get_canvas(frame, toolfig)
@@ -1710,7 +1737,7 @@ class StatusBarWx(wx.StatusBar):
17101737
"""
17111738

17121739
def __init__(self, parent):
1713-
wx.StatusBar.__init__(self, parent, -1)
1740+
wx.StatusBar.__init__(self, parent)
17141741
self.SetFieldsCount(2)
17151742
self.SetStatusText("None", 1)
17161743
# self.SetStatusText("Measurement: None", 2)

lib/matplotlib/backends/backend_wxagg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
class FigureFrameWxAgg(FigureFrameWx):
1818
def get_canvas(self, fig):
19-
return FigureCanvasWxAgg(self, -1, fig)
19+
canvas = FigureCanvasWxAgg(fig)
20+
canvas.Reparent(self)
21+
return canvas
2022

2123

2224
class FigureCanvasWxAgg(FigureCanvasAgg, _FigureCanvasWxBase):

lib/matplotlib/backends/backend_wxcairo.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
import six
55

66
import wx
7+
import wx.lib.wxcairo as wxcairo
78

89
from .backend_cairo import cairo, FigureCanvasCairo, RendererCairo
910
from .backend_wx import (
1011
_BackendWx, _FigureCanvasWxBase, FigureFrameWx,
1112
NavigationToolbar2Wx as NavigationToolbar2WxCairo)
12-
import wx.lib.wxcairo as wxcairo
1313

1414

1515
class FigureFrameWxCairo(FigureFrameWx):
1616
def get_canvas(self, fig):
17-
return FigureCanvasWxCairo(self, -1, fig)
17+
canvas = FigureCanvasWxCairo(fig)
18+
canvas.Reparent(self)
19+
return canvas
1820

1921

2022
class FigureCanvasWxCairo(_FigureCanvasWxBase, FigureCanvasCairo):
@@ -27,12 +29,13 @@ class FigureCanvasWxCairo(_FigureCanvasWxBase, FigureCanvasCairo):
2729
we give a hint as to our preferred minimum size.
2830
"""
2931

30-
def __init__(self, parent, id, figure):
32+
def __init__(self, *args):
3133
# _FigureCanvasWxBase should be fixed to have the same signature as
3234
# every other FigureCanvas and use cooperative inheritance, but in the
33-
# meantime the following will make do.
34-
_FigureCanvasWxBase.__init__(self, parent, id, figure)
35-
FigureCanvasCairo.__init__(self, figure)
35+
# meantime the following will make do. (`args[-1]` is always the
36+
# figure.)
37+
_FigureCanvasWxBase.__init__(self, *args)
38+
FigureCanvasCairo.__init__(self, args[-1])
3639
self._renderer = RendererCairo(self.figure.dpi)
3740

3841
def draw(self, drawDC=None):

0 commit comments

Comments
 (0)