Python 何时在 tkinter 中使用包或网格布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4401202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use pack or grid layouts in tkinter?
提问by Malcolm
Are there any best practice tips regarding when one should use pack vs. grid for their layouts?
是否有关于何时应该使用 pack 与 grid 布局的最佳实践提示?
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.
为了开始对话,似乎有一个偏爱网格与包的用例是当人们想要显示/隐藏小部件时。
采纳答案by Bryan Oakley
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.
两者本质上都不优于另一个。每个人都有优点和缺点。了解这些是什么,选择使用哪个变得显而易见。
gridis considerably easier to use if you need to lay things out in a grid. packis 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.
grid如果您需要在网格中布置事物,则使用起来要容易得多。pack如果您需要做的只是将一些小部件放在单行或单列中,则通常更易于使用。中间有很多灰色区域,其中任何一个都不一定比另一个更好。
The other thing to consider is what you said in your question: if you want to show and hide widgets at run-time, gridis probably the best choice because of the grid_removemethod which remembers the values of all of the configured attributes in case you want to re-add the widget.
要考虑的另一件事是您在问题中所说的内容:如果您想在运行时显示和隐藏小部件,grid这可能是最佳选择,因为该grid_remove方法会记住所有已配置属性的值,以防万一重新添加小部件。
Personally, my first choice is always to use packbecause I first learned Tk back when there was no gridcommand. 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.
就我个人而言,我的第一选择总是使用,pack因为我在没有grid命令的时候第一次学习了 Tk 。如果我不能轻易做pack,否则我很清楚铺设的事情了在网格中,我将使用grid。
回答by MatrixFrog
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 timein the same container. Thank you Bryan Oakley for making that distinction.
我个人只是认为网格更容易使用,所以我会使用它。当然,您可能已经读过您永远不应该做的一件事是尝试在同一个容器中同时使用两者。感谢 Bryan Oakley 做出这样的区分。
回答by Jeff
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.
对于抛光应用,我总是推荐网格覆盖。只有少数边缘情况下包装更容易且符合要求(所有物品都在一行或一列中)。grid 具有更好的“可组合性”(例如,megawidgets 或网格元素的网格元素)。喜欢 grid 的原因是它提供了额外的微调选项。使用权重(影响增长和缩小顺便说一句),最小大小和最大大小,以及诸如强制统一行/列之类的便利功能。
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.
顺便说一句,pack 和 grid 都可以显示/隐藏子元素,尽管两者的语法略有不同。网格稍微好一点,因为“删除”(而不是“忘记”)会记住从属小部件上的网格选项。

