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

Skip to content

Commit 6f2f139

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 6f2f139

File tree

4 files changed

+61
-25
lines changed

4 files changed

+61
-25
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 & 18 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)
@@ -1279,18 +1294,25 @@ class FigureManagerWx(FigureManagerBase):
12791294
def __init__(self, canvas, num, frame):
12801295
DEBUG_MSG("__init__()", 1, self)
12811296
FigureManagerBase.__init__(self, canvas, num)
1282-
self.frame = frame
12831297
self.window = frame
1284-
1285-
self.tb = frame.GetToolBar()
1286-
self.toolbar = self.tb # consistent with other backends
1298+
self.toolbar = frame.GetToolBar()
12871299

12881300
def notify_axes_change(fig):
12891301
'this will be called whenever the current axes is changed'
1290-
if self.tb is not None:
1291-
self.tb.update()
1302+
if self.toolbar is not None:
1303+
self.toolbar.update()
12921304
self.canvas.figure.add_axobserver(notify_axes_change)
12931305

1306+
@property
1307+
@cbook.deprecated("3.0")
1308+
def frame(self):
1309+
return self.window
1310+
1311+
@property
1312+
@cbook.deprecated("3.0")
1313+
def tb(self):
1314+
return self.toolbar
1315+
12941316
def show(self):
12951317
self.frame.Show()
12961318
self.canvas.draw()
@@ -1477,7 +1499,8 @@ def __init__(self, targetfig):
14771499
wx.Frame.__init__(self, None, -1, "Configure subplots")
14781500

14791501
toolfig = Figure((6, 3))
1480-
canvas = FigureCanvasWx(self, -1, toolfig)
1502+
canvas = FigureCanvasWx(toolfig)
1503+
canvas.Reparent(self)
14811504

14821505
# Create a figure manager to manage things
14831506
figmgr = FigureManager(canvas, 1, self)
@@ -1493,7 +1516,7 @@ def __init__(self, targetfig):
14931516

14941517
class NavigationToolbar2Wx(NavigationToolbar2, wx.ToolBar):
14951518
def __init__(self, canvas):
1496-
wx.ToolBar.__init__(self, canvas.GetParent(), -1)
1519+
wx.ToolBar.__init__(self, canvas.GetParent())
14971520
NavigationToolbar2.__init__(self, canvas)
14981521
self.canvas = canvas
14991522
self._idle = True
@@ -1506,7 +1529,9 @@ def __init__(self, canvas):
15061529
self.retinaFix = 'wxMac' in wx.PlatformInfo
15071530

15081531
def get_canvas(self, frame, fig):
1509-
return type(self.canvas)(frame, -1, fig)
1532+
canvas = type(self.canvas)(fig)
1533+
canvas.Reparent(frame)
1534+
return canvas
15101535

15111536
def _init_toolbar(self):
15121537
DEBUG_MSG("_init_toolbar", 1, self)
@@ -1537,7 +1562,7 @@ def pan(self, *args):
15371562
NavigationToolbar2.pan(self, *args)
15381563

15391564
def configure_subplots(self, evt):
1540-
frame = wx.Frame(None, -1, "Configure subplots")
1565+
frame = wx.Frame(None, title="Configure subplots")
15411566

15421567
toolfig = Figure((6, 3))
15431568
canvas = self.get_canvas(frame, toolfig)
@@ -1710,7 +1735,7 @@ class StatusBarWx(wx.StatusBar):
17101735
"""
17111736

17121737
def __init__(self, parent):
1713-
wx.StatusBar.__init__(self, parent, -1)
1738+
wx.StatusBar.__init__(self, parent)
17141739
self.SetFieldsCount(2)
17151740
self.SetStatusText("None", 1)
17161741
# 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)