I have a program with some Label() widgets, some Button() widgets, some Text() widgets, and a few Entry() widgets. A couple of revisions ago, I didn't have the labels, and I had less Entry() widgets, and I mixed .pack() and .grid() as was convenient and I was fine. I had to do some refactoring, and added the extra widgets in the process - all the new things added used .grid(). Nothing about the other widgets changed. Now, I get an error along the lines of "unable to use grid in .", etc. (I can post the full error message if necessary). Why, and how can I fix this? (I can post code if necessary as well.)
1 Answers
You can't mix grid
and pack
with widgets that share the same parent.
Why? Because grid
will try to lay widgets out, possibly growing or shrinking widgets according to various options. Next, pack
will try to do the same according to its rules. This may require that it change widget widths or heights.
grid
will see that widgets have changed size so it will try to rearrange the widgets accirding to its rules. pack
will then notice that some widgets have changed size so it will rearrange the widgets according to its rules. grid
will see that widgets have changed size so it will try to rearrange the widgets according to their rules. pack
will then notice that some widgets have changed size so it will rearrange the widgets according to its rules. grid
will see that ...

- 736
- 7
- 17

- 370,779
- 53
- 539
- 685
-
Would you explain what you mean by "parent"? Apologies, I'm really new to tkinter. – Auden Young Jun 23 '17 at 00:38
-
@heather some people and documentation use the word "master". Widgets exist in a tree-like hierarchy. Every widget except the root widget have a parent. – Bryan Oakley Jun 23 '17 at 01:24
-
@BrianOakley would you mind listing the parents of the widgets I'm using? – Auden Young Jun 23 '17 at 20:00
-
@heather: you've shown no code, so no, I can't. If you haven't explicitly set a parent (and you should), then the parent is the root window. If you didn't explicitly create a root window (and you should), one is created for you. – Bryan Oakley Jun 23 '17 at 20:03
-
okay then, so basically I need to set different parents for the pack and the grid widgets - I have created a root window, but not explicitly created parents. – Auden Young Jun 23 '17 at 20:09
-
I just looked around SO and found https://stackoverflow.com/questions/6285648/can-you-change-a-widgets-parent-in-python-tkinter which seems to imply that you *can't* explicitly set a parent for widgets. – Auden Young Jun 23 '17 at 20:12
-
@heather: correct. _once a widget has been created_ you can't change the parent. However, _at the time the widget is created_ you must either give it a parent, or the parent will be set to the root window. – Bryan Oakley Jun 23 '17 at 20:18