什么时候在tkinter中使用包或网格布局? [英] When to use pack or grid layouts in tkinter?

查看:99
本文介绍了什么时候在tkinter中使用包或网格布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在关于何时应使用包式或网格式布局的最佳实践提示?

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

从我一直在Google上阅读的内容来看,共识似乎是网格可以处理任何打包情况,反之亦然.

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.

推荐答案

两者在本质上都不比另一个更好.每个都有优点和缺点.了解这些是什么,使用它们的选择就变得显而易见.

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相当容易使用.如果您要做的只是将某些小部件放在一行或一列中,通常更容易使用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. There's a whole lot of gray area in-between where neither is necessarily better than the other.

要考虑的另一件事是您在问题中所说的:如果要在运行时显示和隐藏小部件,由于grid_remove方法会记住所有值,因此grid可能是最佳选择您想要重新添加小部件时配置的属性的数量.

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.

就我个人而言,我的首选始终是使用pack,因为在没有grid命令的情况下,我首先学习了Tk.如果无法在pack中轻松完成此操作,或者如果我很清楚地将内容放置在网格中,则将使用grid.

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.

这篇关于什么时候在tkinter中使用包或网格布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆