8000 Ensure that all toolbar (old/new) subclasses can be init'ed consistently · matplotlib/matplotlib@8cd6017 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8cd6017

Browse files
committed
Ensure that all toolbar (old/new) subclasses can be init'ed consistently
i.e. with the same signature: with `canvas` as sole arg for the old-style toolbars, with `toolmanager` as sole arg for the new ones. Subclasses that explicitly support setting a parent widget keep that support (except for gtk, which stashed that in the `.win` attribute but never used it), but that argument is always optional now; the default is the canvas' parent. The goal is to later replace all _get_toolbar implementations by a simple call (always with the same signature (dependent on the value of rcParams["toolbar"])) to the correct class in the FigureManagerBase constructor.
1 parent 719c6b7 commit 8cd6017

File tree

6 files changed

+53
-32
lines changed

6 files changed

+53
-32
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
``NavigationToolbar2GTK3`` window
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
The *window* parameter to ``NavigationToolbar2GTK3`` had no effect, and is now
4+
deprecated; likewise, the ``win`` attribute is deprecated.
5+
6+
``NavigationToolbar2Tk`` window
7+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8+
The *window* attribute to ``NavigationToolbar2GTK3`` is deprecated. Use
9+
``toolbar.master`` instead.

lib/matplotlib/backends/_backend_tk.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,9 @@ def __init__(self, canvas, num, window):
432432

433433
def _get_toolbar(self):
434434
if mpl.rcParams['toolbar'] == 'toolbar2':
435-
toolbar = NavigationToolbar2Tk(self.canvas, self.window)
435+
toolbar = NavigationToolbar2Tk(self.canvas)
436436
elif mpl.rcParams['toolbar'] == 'toolmanager':
437-
toolbar = ToolbarTk(self.toolmanager, self.window)
437+
toolbar = ToolbarTk(self.toolmanager)
438438
else:
439439
toolbar = None
440440
return toolbar
@@ -506,24 +506,26 @@ def full_screen_toggle(self):
506506

507507

508508
class NavigationToolbar2Tk(NavigationToolbar2, tk.Frame):
509-
"""
510-
Attributes
511-
----------
512-
canvas : `FigureCanvas`
513-
The figure canvas on which to operate.
514-
win : tk.Window
515-
The tk.Window which owns this toolbar.
516-
pack_toolbar : bool, default: True
517-
If True, add the toolbar to the parent's pack manager's packing list
518-
during initialization with ``side='bottom'`` and ``fill='x'``.
519-
If you want to use the toolbar with a different layout manager, use
520-
``pack_toolbar=False``.
521-
"""
522-
def __init__(self, canvas, window, *, pack_toolbar=True):
523-
# Avoid using self.window (prefer self.canvas.get_tk_widget().master),
524-
# so that Tool implementations can reuse the methods.
525-
self.window = window
509+
window = _api.deprecated("3.6", alternative="self.master")(
510+
property(lambda self: self.master))
511+
512+
def __init__(self, canvas, window=None, *, pack_toolbar=True):
513+
"""
514+
Parameters
515+
----------
516+
canvas : `FigureCanvas`
517+
The figure canvas on which to operate.
518+
window : tk.Window
519+
The tk.Window which owns this toolbar.
520+
pack_toolbar : bool, default: True
521+
If True, add the toolbar to the parent's pack manager's packing
522+
list during initialization with ``side="bottom"`` and ``fill="x"``.
523+
If you want to use the toolbar with a different layout manager, use
524+
``pack_toolbar=False``.
525+
"""
526526

527+
if window is None:
528+
window = canvas.get_tk_widget().master
527529
tk.Frame.__init__(self, master=window, borderwidth=2,
528530
width=int(canvas.figure.bbox.width), height=50)
529531

@@ -809,8 +811,10 @@ def set_cursor(self, cursor):
809811

810812

811813
class ToolbarTk(ToolContainerBase, tk.Frame):
812-
def __init__(self, toolmanager, window):
814+
def __init__(self, toolmanager, window=None):
813815
ToolContainerBase.__init__(self, toolmanager)
816+
if window is None:
817+
window = self.toolmanager.canvas.get_tk_widget().master
814818
xmin, xmax = self.toolmanager.canvas.figure.bbox.intervalx
815819
height, width = 50, xmax - xmin
816820
tk.Frame.__init__(self, master=window,

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ def _get_toolbar(self):
393393
# must be inited after the window, drawingArea and figure
394394
# attrs are set
395395
if mpl.rcParams['toolbar'] == 'toolbar2':
396-
toolbar = NavigationToolbar2GTK3(self.canvas, self.window)
396+
toolbar = NavigationToolbar2GTK3(self.canvas)
397397
elif mpl.rcParams['toolbar'] == 'toolmanager':
398398
toolbar = ToolbarGTK3(self.toolmanager)
399399
else:
@@ -425,8 +425,9 @@ def resize(self, width, height):
425425

426426

427427
class NavigationToolbar2GTK3(_NavigationToolbar2GTK, Gtk.Toolbar):
428-
def __init__(self, canvas, window):
429-
self.win = window
428+
@_api.delete_parameter("3.6", "window")
429+
def __init__(self, canvas, window=None):
430+
self._win = window
430431
GObject.GObject.__init__(self)
431432

432433
self.set_style(Gtk.ToolbarStyle.ICONS)
@@ -474,6 +475,8 @@ def __init__(self, canvas, window):
474475

475476
_NavigationToolbar2GTK.__init__(self, canvas)
476477

478+
win = _api.deprecated("3.6")(property(lambda self: self._win))
479+
477480
def save_figure(self, *args):
478481
dialog = Gtk.FileChooserDialog(
479482
title="Save the figure",

lib/matplotlib/backends/backend_gtk4.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def _get_toolbar(self):
339339
# must be inited after the window, drawingArea and figure
340340
# attrs are set
341341
if mpl.rcParams['toolbar'] == 'toolbar2':
342-
toolbar = NavigationToolbar2GTK4(self.canvas, self.window)
342+
toolbar = NavigationToolbar2GTK4(self.canvas)
343343
elif mpl.rcParams['toolbar'] == 'toolmanager':
344344
toolbar = ToolbarGTK4(self.toolmanager)
345345
else:
@@ -364,8 +364,9 @@ def resize(self, width, height):
364364

365365

366366
class NavigationToolbar2GTK4(_NavigationToolbar2GTK, Gtk.Box):
367-
def __init__(self, canvas, window):
368-
self.win = window
367+
@_api.delete_parameter("3.6", "window")
368+
def __init__(self, canvas, window=None):
369+
self._win = window
369370
Gtk.Box.__init__(self)
370371

371372
self.add_css_class('toolbar')
@@ -407,6 +408,8 @@ def __init__(self, canvas, window):
407408

408409
_NavigationToolbar2GTK.__init__(self, canvas)
409410

411+
win = _api.deprecated("3.6")(property(lambda self: self._win))
412+
410413
def save_figure(self, *args):
411414
dialog = Gtk.FileChooserNative(
412415
title='Save the figure',

lib/matplotlib/backends/backend_qt.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,9 @@ def _get_toolbar(self, canvas, parent):
586586
# must be inited after the window, drawingArea and figure
587587
# attrs are set
588588
if mpl.rcParams['toolbar'] == 'toolbar2':
589-
toolbar = NavigationToolbar2QT(canvas, parent, True)
589+
< 10000 span class="pl-s1">toolbar = NavigationToolbar2QT(canvas)
590590
elif mpl.rcParams['toolbar'] == 'toolmanager':
591-
toolbar = ToolbarQt(self.toolmanager, self.window)
591+
toolbar = ToolbarQt(self.toolmanager)
592592
else:
593593
toolbar = None
594594
return toolbar
@@ -637,7 +637,7 @@ class NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar):
637637
("Customize", "Edit axis, curve and image parameters",
638638
"qt4_editor_options", "edit_parameters"))
639639

640-
def __init__(self, canvas, parent, coordinates=True):
640+
def __init__(self, canvas, parent=None, coordinates=True):
641641
"""coordinates: should we show the coordinates on the right?"""
642642
QtWidgets.QToolBar.__init__(self, parent)
643643
self.setAllowedAreas(QtCore.Qt.ToolBarArea(
@@ -909,7 +909,7 @@ def _reset(self):
909909

910910

911911
class ToolbarQt(ToolContainerBase, QtWidgets.QToolBar):
912-
def __init__(self, toolmanager, parent):
912+
def __init__(self, toolmanager, parent=None):
913913
ToolContainerBase.__init__(self, toolmanager)
914914
QtWidgets.QToolBar.__init__(self, parent)
915915
self.setAllowedAreas(QtCore.Qt.ToolBarArea(

lib/matplotlib/backends/backend_wx.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ def _get_toolbar(self):
928928
if mpl.rcParams['toolbar'] == 'toolbar2':
929929
toolbar = NavigationToolbar2Wx(self.canvas)
930930
elif mpl.rcParams['toolbar'] == 'toolmanager':
931-
toolbar = ToolbarWx(self.toolmanager, self)
931+
toolbar = ToolbarWx(self.toolmanager)
932932
else:
933933
toolbar = None
934934
return toolbar
@@ -1190,7 +1190,9 @@ def set_history_buttons(self):
11901190
# tools for matplotlib.backend_managers.ToolManager:
11911191

11921192
class ToolbarWx(ToolContainerBase, wx.ToolBar):
1193-
def __init__(self, toolmanager, parent, style=wx.TB_BOTTOM):
1193+
def __init__(self, toolmanager, parent=None, style=wx.TB_BOTTOM):
1194+
if parent is None:
1195+
parent = toolmanager.canvas.GetParent()
11941196
ToolContainerBase.__init__(self, toolmanager)
11951197
wx.ToolBar.__init__(self, parent, -1, style=style)
11961198
self._space = self.AddStretchableSpace()

0 commit comments

Comments
 (0)
0