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 b334fb71a763..c5b12e002006 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))