_tkinter.TclError: cannot use geometry manager pack inside

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# _tkinter.TclError: cannot use geometry manager pack inside

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().

tkinter tclerror cannot use geometry manager pack inside

Here is an example of how the error occurs.

main.py
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.

main.py
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.

main.py
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.

# Only using the grid() method when positioning Widgets

One way to solve the error is to only use the grid() method when positioning widgets.

main.py
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()

only using grid method to position widgets

The code for this article is available on GitHub

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:

  • Make sure the correct parent is used for all widgets

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.

main.py
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.

# Only using the pack() method when positioning Widgets

Alternatively, you can only use the pack() method when positioning widgets.

main.py
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()
The code for this article is available on GitHub

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.

# On mixing pack() and grid()

Here is an example that uses grid() to position all of the widgets into a Frame and then uses pack() to pack the Frame.

main.py
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()
The code for this article is available on GitHub
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().

Notice that the 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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev