Python tkinter pack 方法的“填充”和“扩展”选项之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28089942/
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
Difference between "fill" and "expand" options for tkinter pack method
提问by Python_user
I know this is a too trivial question, but I am new to python, and I have just started using the tkinter
module. I have actually looked up about it everywhere, and I am unable to find the satisfactory answer. I found the following:
我知道这是一个太琐碎的问题,但我是 Python 新手,而且我刚刚开始使用该tkinter
模块。其实我到处查了查,都找不到满意的答案。我发现了以下内容:
fill
option: it determines whether to use up more space or keep "one's own" dimensions.
expand
option: it deals with the expansion of parent widget.
fill
选项:它决定是使用更多空间还是保留“自己的”尺寸。
expand
选项:它处理父小部件的扩展。
The problemis that these two sound more or less the same. I even tried out a few examples by toggling between the 4 values of fill
and 2 values of expand
but received more or less the same output in 2 or 3 cases, because of which I have this query. Any help would be appreciated in this regards. Thanks in advance!
该问题是,这两个声音或多或少相同。我什至通过在 的 4 个值fill
和 2 个值之间切换来尝试一些示例,expand
但在 2 或 3 种情况下收到或多或少相同的输出,因此我有这个查询。在这方面的任何帮助将不胜感激。提前致谢!
采纳答案by fhdrsdg
From effbot:
从effbot:
The filloption tells the manager that the widget wants fill the entire space assigned to it. The value controls how to fill the space; BOTHmeans that the widget should expand both horizontally and vertically, Xmeans that it should expand only horizontally, and Ymeans that it should expand only vertically.
The expandoption tells the manager to assign additional space to the widget box. If the parent widget is made larger than necessary to hold all packed widgets, any exceeding space will be distributed among all widgets that have the expandoption set to a non-zero value.
该填充选项告诉管理器窗口小部件的欲望填充分配给它的整个空间。该值控制如何填充空间;BOTH意味着小部件应该水平和垂直扩展,X意味着它应该只水平扩展,Y意味着它应该只垂直扩展。
该扩展选项告诉经理分配额外的空间给小部件箱。如果父小部件比容纳所有打包小部件所需的大,则任何超出的空间都将分配给所有将expand选项设置为非零值的小部件。
So fill
tells the widget to grow to as much space is available for it in the direction specified, expand
tells the master to take any space that is not assigned to any widget and distribute it to all widgets that have a non-zero expand
value.
所以fill
告诉小部件在指定的方向上增长到尽可能多的可用空间,expand
告诉主控采取任何未分配给任何小部件的空间并将其分配给所有具有非零expand
值的小部件。
The difference becomes clear when running this example:
运行此示例时,差异变得很明显:
import Tkinter as tk
root = tk.Tk()
root.geometry('200x200+200+200')
tk.Label(root, text='Label', bg='green').pack(expand=1, fill=tk.Y)
tk.Label(root, text='Label2', bg='red').pack(fill=tk.BOTH)
root.mainloop()
You can see that the label with expand=1
gets assigned as much space as available for it, but only occupies it in the direction specified, Y.
The label with fill=tk.BOTH
expands in both directions, but has less space available.
您可以看到标签 withexpand=1
被分配了尽可能多的可用空间,但仅在指定的方向 Y 上占用它。标签fill=tk.BOTH
在两个方向上都扩展,但可用空间较少。