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 vic开发者_开发知识库e-versa.
To start the conversation, it appears that one use case that favors grid vs. pack is when one wants to show/hide widgets.
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
.
I always recommend grid over pack for polished applications. There are only a few edge cases where pack is easier and fits the bill (everything in one row or col). grid has better "composability" (e.g. megawidgets or gridding elements of gridded elements). The reasons to prefer grid are the extra fine-tuning options that it provides. The use of weight (which effects growing and shrinking btw), minsize and maxsize, as well as convenience features like enforcing uniform rows/columns.
A fully gridded app of any size will use (significantly) fewer frames than an equivalent packed app, and have better shrink/expand control over inner elements.
BTW, both pack and grid can show/hide sub-elements, though the syntax differs slightly between the two. Grid is just slightly better because 'remove' (rather than 'forget') will remember the grid options on the slave widget.
I personally just think grid is a lot easier to work with, so I would use that. Of course, you've probably read the one thing you should never do is try to use both at the same time in the same container. Thank you Bryan Oakley for making that distinction.
精彩评论