何时在 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两者本质上并不比另一个更好。每个人都有优点和缺点。了解它们是什么,然后选择使用哪个就变得显而易见了。
如果您需要将内容放置在网格中,
grid
使用起来要容易得多。如果您所需要做的只是将一些小部件放在单行或单列中,那么pack
通常更容易使用。两者之间存在大量的灰色地带,其中任何一个都不一定比另一个更好。另一件要考虑的事情是你在问题中所说的:如果你想在运行时显示和隐藏小部件,
grid
可能是最好的选择,因为grid_remove
方法,它会记住所有已配置属性的值,以防您想要重新添加小部件。就我个人而言,我的第一选择始终是使用
pack
,因为我第一次学习 Tk 是在没有grid
命令的时候。如果我无法在pack
中轻松做到这一点,或者如果我非常清楚地在网格中布局,我将使用grid
。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 thegrid_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 nogrid
command. If I can't do it easily inpack
, or if I'm very clearly laying things out in a grid, I'll usegrid
.对于抛光应用,我始终推荐网格优于包装。只有少数边缘情况下,打包更容易并且符合要求(所有内容都在一行或一列中)。网格具有更好的“可组合性”(例如,大型小部件或网格元素的网格元素)。更喜欢网格的原因是它提供的额外微调选项。使用权重(顺便说一下,它会影响增长和收缩)、最小尺寸和最大尺寸,以及诸如强制行/列统一等便利功能。
任何大小的完全网格应用程序将比同等的打包应用程序使用(显着)更少的框架,并且对内部元素有更好的收缩/扩展控制。
顺便说一句, pack 和 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.
我个人认为网格更容易使用,所以我会使用它。当然,您可能已经读过,您永远不应该做的一件事是尝试在同一个容器中
同时使用两者。感谢 Bryan Oakley 做出的区分。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.