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

Skip to content

Commit f49b7d1

Browse files
authored
Merge pull request #15274 from jmehne/tk-toolbar-grid
NavigationToolbar2Tk: make packing optional.
2 parents abfd0be + 9737747 commit f49b7d1

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

examples/user_interfaces/embedding_in_tk_sgskip.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
2727
canvas.draw()
2828

29-
toolbar = NavigationToolbar2Tk(canvas, root)
29+
# pack_toolbar=False will make it easier to use a layout manager later on.
30+
toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
3031
toolbar.update()
3132

3233

@@ -44,6 +45,7 @@ def on_key_press(event):
4445
# The canvas is rather flexible in its size, so we pack it last which makes
4546
# sure the UI controls are displayed as long as possible.
4647
button.pack(side=tkinter.BOTTOM)
48+
toolbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
4749
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
4850

4951
tkinter.mainloop()

lib/matplotlib/backends/_backend_tk.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,17 +507,23 @@ class NavigationToolbar2Tk(NavigationToolbar2, tk.Frame):
507507
Attributes
508508
----------
509509
canvas : `FigureCanvas`
510-
the figure canvas on which to operate
510+
The figure canvas on which to operate.
511511
win : tk.Window
512-
the tk.Window which owns this toolbar
513-
512+
The tk.Window which owns this toolbar.
513+
pack_toolbar : bool, default: True
514+
If True, add the toolbar to the parent's pack manager's packing list
515+
during initialization with ``side='bottom'`` and ``fill='x'``.
516+
If you want to use the toolbar with a different layout manager, use
517+
``pack_toolbar=False``.
514518
"""
515-
def __init__(self, canvas, window):
519+
def __init__(self, canvas, window, *, pack_toolbar=True):
516520
self.canvas = canvas
517521
# Avoid using self.window (prefer self.canvas.get_tk_widget().master),
518522
# so that Tool implementations can reuse the methods.
519523
self.window = window
520524
NavigationToolbar2.__init__(self, canvas)
525+
if pack_toolbar:
526+
self.pack(side=tk.BOTTOM, fill=tk.X)
521527

522528
def destroy(self, *args):
523529
del self.message
@@ -582,7 +588,6 @@ def _init_toolbar(self):
582588
self.message = tk.StringVar(master=self)
583589
self._message_label = tk.Label(master=self, textvariable=self.message)
584590
self._message_label.pack(side=tk.RIGHT)
585-
self.pack(side=tk.BOTTOM, fill=tk.X)
586591

587592
def configure_subplots(self):
588593
toolfig = Figure(figsize=(6, 3))

0 commit comments

Comments
 (0)