Skip to content

NavigationToolbar2Tk: make packing optional. #15274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/user_interfaces/embedding_in_tk_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -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()
15 changes: 10 additions & 5 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down