Closed
Description
Bug summary
Using matplotlib backend for a navigation toolbar in tkinter with grid
package manager gives the error:
_tkinter.TclError: cannot use geometry manager pack inside .!frame which already has slaves managed by grid
One should be able to use either package managers pack
or grid
ideally.
Code for reproduction
import tkinter
import tkinter as tk
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
window = tkinter.Tk()
window.wm_title("MRI in Tk")
frame = ttk.Frame(window)
frame.grid(row=0, column=0, sticky="nsew")
fig = Figure(figsize=(8,8))
pc_label = FigureCanvasTkAgg(fig, master=frame)
pc_label.get_tk_widget().grid(row=0, column=1)
pc_toolbar_label = NavigationToolbar2Tk(pc_label, frame)
pc_toolbar_label.grid(row=0, column=1, sticky="s")
Actual outcome
Traceback (most recent call last):
File "/home/varora/PythonProjects/gui/tmp.py", line 16, in <module>
pc_toolbar_label = NavigationToolbar2Tk(pc_label, frame)
File "/home/varora/anaconda3/envs/nnunet_venv/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line 657, in __init__
self.pack(side=tk.BOTTOM, fill=tk.X)
File "/home/varora/anaconda3/envs/nnunet_venv/lib/python3.10/tkinter/__init__.py", line 2425, in pack_configure
self.tk.call(
_tkinter.TclError: cannot use geometry manager pack inside .!frame which already has slaves managed by grid
Expected outcome
Additional information
This script uses a matplotlib
backend for tkinter
for displaying the navigation toolbar which natively implements
the package manager pack()
for tkinter
. Since we use grid()
package manager, this creates a conflict and hence
you need to change the Line: 657
in [PYTHON-DIR]/site-packages/matplotlib/backends/_backend_tk.py
.
One should be able to use either package managers ideally.
Operating system
Ubuntu
Matplotlib Version
3.7.2
Matplotlib Backend
module://backend_interagg
Python version
3.10
Jupyter version
No response
Installation
pip