Python, cannot use geometry manager pack inside [duplicate] Python, cannot use geometry manager pack inside [duplicate] tkinter tkinter

Python, cannot use geometry manager pack inside [duplicate]


This error can occur if you mix pack() and grid() in the same master window. According the docs, it is a bad idea:

Warning: Never mix grid and pack in the same master window. Tkinter will happily spend the rest of your lifetime trying to negotiate a solution that both managers are happy with. Instead of waiting, kill the application, and take another look at your code. A common mistake is to use the wrong parent for some of the widgets.

For example, this code is working for Python 3.3.x, but isn't working on Python 3.4.x (throws the error you mentioned):

from tkinter import *from tkinter import ttkroot = Tk()mainframe = ttk.Frame(root)mainframe.grid(column=0, row=0, sticky=(N, W, E, S))nb = ttk.Notebook(root)nb.pack()root.mainloop()

And this code isn't working for both Python versions:

from tkinter import *root = Tk()Label(root, text="First").grid(row=0)Label(root, text="Second").pack()root.mainloop()

To avoid this, use only one geometry manager for all children of a given parent, such as grid().


The error means that you're doing something like this:

widget1 = tk.Label(root, ...)widget2 = tk.Label(root, ...)widget1.grid(...)widget2.pack(...)

You cannot mix both pack and grid on widgets that have the same parent. It might work on some older versions of tkinter, but only if you're lucky. The solution is straight-foward: switch to using only grid, or only pack, for all widgets that share the same parent.

The code was appearing to work in one version but not the other likely because the later version uses a newer version of tkinter. Tkinter didn't use to give this warning -- it would try to continue running, usually with disastrous results. Whether the program worked or froze was dependent on many factors. Usually the program would freeze and use close to 100% cpu, sometimes it would work, and sometimes it would work until you resized the window. Regardless, it's something you shouldn't do any any version of Tkinter.