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

Skip to content

Commit 214790d

Browse files
committed
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.
1 parent 70c8a4f commit 214790d

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,16 +507,21 @@ 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
524+
self.pack_toolbar = pack_toolbar
520525
NavigationToolbar2.__init__(self, canvas)
521526

522527
def destroy(self, *args):
@@ -582,7 +587,8 @@ def _init_toolbar(self):
582587
self.message = tk.StringVar(master=self)
583588
self._message_label = tk.Label(master=self, textvariable=self.message)
584589
self._message_label.pack(side=tk.RIGHT)
585-
self.pack(side=tk.BOTTOM, fill=tk.X)
590+
if self.pack_toolbar:
591+
self.pack(side=tk.BOTTOM, fill=tk.X)
586592

587593
def configure_subplots(self):
588594
toolfig = Figure(figsize=(6, 3))

0 commit comments

Comments
 (0)