Grid and Pack in Main and Class?
It is a best practice to use both grid
and pack
within an application.
The only caveat is that you cannot mix them within a given frame. The choice of geometry manager (grid
, pack
, place
) for widgets in a frame is completely independent of the geometry manger used in any other frame. Each has strengths and weaknesses, and you should use them accordingly.
Also -- and I see this mistake all the time -- a class that inherits from a widget should not call grid
or pack
or place
on itself.
For example, this is incorrect:
class Example(tk.Frame): def __init__(self, parent): ... self.pack(...)class Main(tk.Frame): def __init__(self, parent): ... this.example = Example(self)
Instead of the above, pack
should be removed from Example
, and Main
should be redefined to call pack
(or grid
or place
):
class Main(tk.Frame): def __init__(self, parent): ... this.example = Example(self) this.example.pack(...)
The problem with the first (incorrect) example is that if you have a dozen frames inside of main along with some other widgets, if you decide to switch from pack
to grid
, you'll have to modify a dozen other classes. By having a parent responsible for arranging it's own children, when you make a layout change, you only have to change a single function.