Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use pack or grid layouts in tkinter?

Are there any best practice tips regarding when one should use pack vs. grid for their layouts?

From what I've been reading via google, the concencus seems to be that grid can handle any pack scenario but not vice-versa.

To start the conversation, it appears that one use case that favors grid vs. pack is when one wants to show/hide widgets.

like image 494
Malcolm Avatar asked Dec 09 '10 17:12

Malcolm


People also ask

Should I use grid or pack Tkinter?

pack is the easiest layout manager to use with Tkinter. However, pack() is limited in precision compared to place() and grid() which feature absolute positioning. For simple positioning of widgets vertically or horizontally in relation to each other, pack() is the layout manager of choice.

Should I use grid or pack?

grid is considerably easier to use if you need to lay things out in a grid. pack is generally easier to use if all you need to do is put some widgets in a single row or single column.

Can you use both grid and pack in Tkinter?

Tkinter has three built-in layout managers: the pack , grid , and place managers. The place geometry manager positions widgets using absolute positioning. The pack geometry manager organizes widgets in horizontal and vertical boxes. The grid geometry manager places widgets in a two dimensional grid.

Why do we use pack in Tkinter?

pack is the easiest Layout Manager to code with in Tkinter. Instead of declaring the precise location of a widget, the pack() method declares the position of widgets in relation to each other. However, pack() is limited in precision compared to place() and grid() which feature absolute positioning.


1 Answers

Neither is intrinsically better than the other. Each have strengths and weaknesses. Learn what those are and the choice of which to use becomes obvious.

grid is considerably easier to use if you need to lay things out in a grid. pack is generally easier to use if all you need to do is put some widgets in a single row or single column. There's a whole lot of gray area in-between where neither is necessarily better than the other.

The other thing to consider is what you said in your question: if you want to show and hide widgets at run-time, grid is probably the best choice because of the grid_remove method which remembers the values of all of the configured attributes in case you want to re-add the widget.

Personally, my first choice is always to use pack because I first learned Tk back when there was no grid command. If I can't do it easily in pack, or if I'm very clearly laying things out in a grid, I'll use grid.

like image 142
Bryan Oakley Avatar answered Oct 02 '22 16:10

Bryan Oakley