From 7362573ae15cff88ffabfadf25be616277a8c4ef Mon Sep 17 00:00:00 2001 From: Julian Mehne Date: Mon, 16 Sep 2019 19:54:56 +0200 Subject: [PATCH 1/2] NavigationToolbar2Tk: make packing optional. Currently, the toolbar cannot be easily used with Tk's grid layout manager, because it is already packed during initialization. Thus, it is necessary to manually call `pack_forget` on the toolbar. Add a parameter, that makes this a bit easier to discover. --- lib/matplotlib/backends/_backend_tk.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index b334fb71a763..ad40213e97bd 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -507,17 +507,23 @@ class NavigationToolbar2Tk(NavigationToolbar2, tk.Frame): Attributes ---------- canvas : `FigureCanvas` - the figure canvas on which to operate + The figure canvas on which to operate. win : tk.Window - the tk.Window which owns this toolbar - + The tk.Window which owns this toolbar. + pack_toolbar : bool, default: True + If True, add the toolbar to the parent's pack manager's packing list + during initialization with *side='bottom'* and *fill='x'*. + If you want to use the toolbar with a different layout manager, use + *pack_toolbar=False*. """ - def __init__(self, canvas, window): + def __init__(self, canvas, window, *, pack_toolbar=True): self.canvas = canvas # Avoid using self.window (prefer self.canvas.get_tk_widget().master), # so that Tool implementations can reuse the methods. self.window = window NavigationToolbar2.__init__(self, canvas) + if pack_toolbar: + self.pack(side=tk.BOTTOM, fill=tk.X) def destroy(self, *args): del self.message @@ -582,7 +588,6 @@ def _init_toolbar(self): self.message = tk.StringVar(master=self) self._message_label = tk.Label(master=self, textvariable=self.message) self._message_label.pack(side=tk.RIGHT) - self.pack(side=tk.BOTTOM, fill=tk.X) def configure_subplots(self): toolfig = Figure(figsize=(6, 3)) From 9737747b3f34541744f7e5557ed67e7f8a2889b3 Mon Sep 17 00:00:00 2001 From: Julian Mehne Date: Mon, 16 Sep 2019 21:44:29 +0200 Subject: [PATCH 2/2] Embedding in Tk example: be more layout manager agnostic. --- examples/user_interfaces/embedding_in_tk_sgskip.py | 4 +++- lib/matplotlib/backends/_backend_tk.py | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/user_interfaces/embedding_in_tk_sgskip.py b/examples/user_interfaces/embedding_in_tk_sgskip.py index ad85b7b320d3..2d9e246f73ef 100644 --- a/examples/user_interfaces/embedding_in_tk_sgskip.py +++ b/examples/user_interfaces/embedding_in_tk_sgskip.py @@ -26,7 +26,8 @@ canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea. canvas.draw() -toolbar = NavigationToolbar2Tk(canvas, root) +# pack_toolbar=False will make it easier to use a layout manager later on. +toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False) toolbar.update() @@ -44,6 +45,7 @@ def on_key_press(event): # The canvas is rather flexible in its size, so we pack it last which makes # sure the UI controls are displayed as long as possible. button.pack(side=tkinter.BOTTOM) +toolbar.pack(side=tkinter.BOTTOM, fill=tkinter.X) canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1) tkinter.mainloop() diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index ad40213e97bd..c5b12e002006 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -512,9 +512,9 @@ class NavigationToolbar2Tk(NavigationToolbar2, tk.Frame): The tk.Window which owns this toolbar. pack_toolbar : bool, default: True If True, add the toolbar to the parent's pack manager's packing list - during initialization with *side='bottom'* and *fill='x'*. + during initialization with ``side='bottom'`` and ``fill='x'``. If you want to use the toolbar with a different layout manager, use - *pack_toolbar=False*. + ``pack_toolbar=False``. """ def __init__(self, canvas, window, *, pack_toolbar=True): self.canvas = canvas