“fill"和“fill"之间的区别和“扩展"tkinter 打包方法的选项 [英] Difference between "fill" and "expand" options for tkinter pack method

查看:35
本文介绍了“fill"和“fill"之间的区别和“扩展"tkinter 打包方法的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个太琐碎的问题,但我是 Python 新手,而且我刚刚开始使用 tkinter 模块.其实我到处查了查,都找不到满意的答案.我发现了以下内容:

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:

fill 选项:决定是使用更多空间还是保留自己的"维度.

fill option: it determines whether to use up more space or keep "one's own" dimensions.

expand 选项:处理父widget的扩展.

expand option: it deals with the expansion of parent widget.

问题是这两个听起来或多或少相同.我什至通过在 fill 的 4 个值和 expand 的 2 个值之间切换来尝试了一些示例,但在 2 或 3 种情况下收到或多或少相同的输出,因为我有这个查询.在这方面的任何帮助将不胜感激.提前致谢!

The problem is 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!

推荐答案

来自 effbot:

fill 选项告诉经理小部件想要填充分配给它的整个空间.该值控制如何填充空间;BOTH 意味着小部件应该水平和垂直展开,X 意味着它应该只水平展开,Y 意味着它应该只展开垂直.

The fill option tells the manager that the widget wants fill the entire space assigned to it. The value controls how to fill the space; BOTH means that the widget should expand both horizontally and vertically, X means that it should expand only horizontally, and Y means that it should expand only vertically.

展开 选项告诉经理为小部件框分配额外的空间.如果父小部件比容纳所有打包小部件所需的空间大,则任何超出的空间都将分配给所有 expand 选项设置为非零值的小部件.

The expand option 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 expand option set to a non-zero value.

所以 fill 告诉小部件在指定的方向上增长到尽可能多的可用空间,expand 告诉主控占用任何未分配的空间任何小部件并将其分发给具有非零 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.

运行此示例时,差异变得很明显:

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()

您可以看到带有 expand=1 的标签被分配了尽可能多的可用空间,但只在指定的方向 Y 上占用它.fill=tk.BOTH 标签向两个方向扩展,但可用空间较少.

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.

这篇关于“fill"和“fill"之间的区别和“扩展"tkinter 打包方法的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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