一些基本的 tkinter 问题 [英] Some elementary tkinter questions
问题描述
我有几个关于几何管理器的问题:
I have a few questions regarding geometry managers:
(a) grid
几何管理器如何确定行和列?我的意思是,网格管理器如何将主小部件索引到行和列中?
(a) How does the grid
geometry managers determine the row and column ? I mean, how does grid manager indexes the master widget into row and column ?
我对此有很多困惑 - 当您输入 a.grid(row=100,column=100)
和 a.grid(row=0, column=0)
,结果一模一样!
I am having lot of confusion regarding this - when you type a.grid(row=100,column=100)
and a.grid(row=0, column=0)
, the result is exactly the same !
另外,你能不能使用 grid
将一个小部件放在最右上角,一个小部件放在最左下角,中间空间为空?
Also, can you place a widget on topmost right and a widget on bottommost left, and the middlespace empty, using grid
?
(b) pack
几何管理器中的 fill
和 expand
有什么区别?
(b) What's the difference between fill
and expand
in pack
geometry manager ?
另外,当我创建 a = Text(root, width=100, height=100)
时,然后输入 a.pack(side=TOP)
或 a.pack(side=LEFT)
或 a.pack(side=RIGHT)
,为什么文本框总是位于页面的底部?
Also, when I create a = Text(root, width=100, height=100)
, then type a.pack(side=TOP)
or a.pack(side=LEFT)
or a.pack(side=RIGHT)
, why does the text box always sits in the bottom of the page ?
推荐答案
a) grid
几何管理器只为实际包含某些内容的行和列分配空间.因此,如果您只有一个小部件,则无论您选择哪一行和哪一列,网格将实际上只有一个单元格(也就是说,除非您使用 grid_rowconfigure
或 grid_columnconfigure 指定其他选项
,更多关于本教程和这里).
a) The grid
geometry manager only assigns space to the rows and columns that actually have some content. Therefore, if you only have one widget it does not matter what row and column you choose, the grid will effectively have one cell only (that is, unless you specify additional options with grid_rowconfigure
or grid_columnconfigure
, more about that in this tutorial and here).
b) 使用 pack
几何管理器,fill
选项指示小部件它必须扩展自己的大小以填充几何管理器分配的空间.如果您不指定它,则小部件周围可能会有一些空白空间,例如,如果您将多个小部件排成一行,并且其中一些比其他小部件高.另一方面,expand
选项会影响几何管理器分配给小部件的空间量;如果您不指定它,它将获得尽可能小的空间量,否则任何剩余空间将分配给所有设置了 expand
的小部件(更多信息 这里).所以它们是真正独立的东西,一个影响小部件本身的大小,另一个影响它接收的窗口空间量.
b) With the pack
geometry manager, the fill
option indicates the widget that it has to expand its own size to fill the space assigned by the geometry manager. If you don't specify it, you may have some empty space surrounding the widget, for example if you put several widgets in a row and some are taller than other. The expand
option, on the other hand, affects the amount of space that the geometry manager assigns to the widget; if you don't specify it, it will receive the minimum amount of space possible, otherwise any remaining space will be distributed among all the widgets that have expand
set (more info here). So they are really independent things, one affects the size of the widget itself and the other the amount of window space it receives.
至于您的最后一个问题,也许您可以提供一个最小示例来说明您的问题.
As for your last question, maybe you can provide a minimal example showing your problem.
这篇关于一些基本的 tkinter 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!