Last updated: Apr 11, 2024
Reading timeยท3 min
The error "_tkinter.TclError: cannot use geometry manager pack inside" occurs
when you mix the pack()
and grid()
methods in the same root window.
To resolve the issue, make sure to only use pack()
or only use grid()
.
Here is an example of how the error occurs.
from tkinter import Tk, ttk root = Tk() frm = ttk.Frame(root, padding=10) frm.grid() label = ttk.Label(frm, text="bobbyhadz.com") # ๐๏ธ Using grid() label.grid(column=0, row=9) button = ttk.Button(root, text="Quit", command=root.destroy) # โ๏ธ _tkinter.TclError: cannot use geometry manager pack inside . which already has slaves managed by grid # ๐๏ธ using pack() button.pack() root.mainloop()
We created a Label
widget with the ttk.Label()
class and used the grid()
method to position the Label
widget in the parent widget in a grid.
label = ttk.Label(frm, text="bobbyhadz.com") # ๐๏ธ Using grid() label.grid(column=0, row=9)
We then created a Button
widget using the ttk.Button()
class and used the
pack()
method to pack the Button
in the parent widget.
button = ttk.Button(root, text="Quit", command=root.destroy) # โ๏ธ _tkinter.TclError: cannot use geometry manager pack inside . which already has slaves managed by grid # ๐๏ธ Using pack() button.pack()
However, mixing the grid()
and pack()
methods in the same root window is not
allowed which caused the error.
pack()
and grid()
cannot be used inside the same class or for the same frame.You can either pick to only use grid()
or pack()
but the two cannot be
mixed.
grid()
method when positioning WidgetsOne way to solve the error is to only use the grid()
method when positioning
widgets.
from tkinter import Tk, ttk root = Tk() frm = ttk.Frame(root, padding=10) frm.grid() label = ttk.Label(frm, text="bobbyhadz.com") # ๐๏ธ Using grid() label.grid(column=0, row=9) button = ttk.Button(root, text="Quit", command=root.destroy) # ๐๏ธ Using grid() button.grid(column=1, row=0) root.mainloop()
This time, we used the grid()
method to position the Label
and the Button
widgets, so everything works as expected.
The column
keyword argument represents the column cell where the widget should
be positioned (starting at 0
).
The row
keyword argument represents the row cell where the widget should be
positioned (starting at 0
).
Another thing to look out for:
The parent is passed as the first argument to the widget classes when instantiating them.
For example, the Label
has the frame as its parent.
from tkinter import Tk, ttk root = Tk() frm = ttk.Frame(root, padding=10) frm.grid() # ๐๏ธ Frame is parent label = ttk.Label(frm, text="bobbyhadz.com") label.grid(column=0, row=9)
Make sure the correct parent is used for all of your widgets as passing an incorrect parent value also causes the error.
pack()
method when positioning WidgetsAlternatively, you can only use the pack()
method when positioning widgets.
from tkinter import Tk, ttk root = Tk() root.geometry('500x300') label = ttk.Label(root, text="bobbyhadz.com") # ๐๏ธ Using grid() label.pack(pady=20) button = ttk.Button(root, text="Quit", command=root.destroy) # ๐๏ธ Using pack() button.pack(padx=40) root.mainloop()
This time we used the pack()
method when positioning the Label
and Button
widgets.
You can pass the padx
(padding X axis) or pady
(padding Y axis) keyword
arguments to the method if you need to set horizontal or vertical padding.
Here is an example that uses grid()
to position all of the widgets into a
Frame
and then uses pack()
to pack the Frame
.
from tkinter import Tk, ttk root = Tk() frm = ttk.Frame(root, padding=10) label = ttk.Label(frm, text="bobbyhadz.com") # ๐๏ธ Using grid() label.grid(column=0, row=0) button = ttk.Button(frm, text="Quit", command=root.destroy) # ๐๏ธ Using grid() button.grid(column=1, row=0) # ๐๏ธ Using pack() frm.pack() root.mainloop()
pack()
and grid()
cannot be used inside the same class or for the same frame.However, you can use grid()
to position your widgets inside the Frame and then
pack the frame with pack()
.
Label
and Button
widgets and the Frame
widgets don't share the same parent.The parent of the Label
and Button
widgets is the Frame
widget.
The parent of the Frame
widget is the root
top-level widget.
Make sure you don't try to use grid()
and pack()
with widgets that share the
same parent as this causes the error.
You can learn more about the related topics by checking out the following tutorials: