2

I am having several errors after the installation of Anaconda. When I run a program I get the message:

TclError: cannot use geometry manager pack inside . which already has slaves managed by grid

The program was written using Python 3.3. The Anaconda version is for 3.4. But I don't think there were any syntax differences between 3.3 and 3.4. I searched and could not find any solution for this error. I don't even know what it means.

Thank you.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

2 Answers2

6

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 ttk

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

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
NorthCat
  • 9,643
  • 16
  • 47
  • 50
3

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685