TKinter - 使用网格布局的小部件不会“粘在"框架中 [英] TKinter - widgets not 'sticking' in Frame using grid layout

查看:30
本文介绍了TKinter - 使用网格布局的小部件不会“粘在"框架中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 TKinter 中构建一个相当复杂的 GUI,所以很自然地一直在使用 .grid 函数.

I am building a fairly complicated GUI in TKinter so naturally have been using the .grid function.

我已将我的一些小部件分组到框架中以使其更易于处理,但出于某种原因,当我将 .grid 与框架中的小部件一起使用时,sticky 属性似乎不起作用 - 我的小部件没有填充整个框架.

I have grouped some of my widgets into Frames to make them easier to handle, but for some reason when I use .grid with widgets in a frame, the sticky attribute does not seem to work - my widgets don't fill the whole frame.

示例:

f3 = tk.Frame(self, borderwidth=1, relief="ridge")
f3.grid(row=0, column=0, sticky="NS")
tk.Label(f3, text="Site List").grid(in_=f3, row=0, column=0)
self.sitelist = tk.Listbox(f3)
self.sitelist.grid(in_=f3, row=1, column=0, sticky="NS")

在上面的代码中,框架 f3 填充了我的根小部件的 0,0 单元格中的空间,但是列表框并没有填充框架中的所有空间,即使我已经问过它是粘性的NS".

In the above code, the frame f3 fills the space in the 0,0 cell of my root widget, but the Listbox does not fill all the space in the Frame, even though I have asked it to be sticky "NS".

如果将列表框放在根小部件中的 0,0 处,那么它会拉伸并填充所有空间.如果它在框架中,它只是表现不佳.

If put the Listbox in the root widget at 0,0 then it stretches and fills all the space fine. It just does not behave well if it is in the Frame.

谁能解释一下如何解决这个问题?

Can anyone explain how to get around this?

我认为使用框架会简化我的布局,但事实并非如此!!!

I thought that using frames would simplify my layout, but it is not!!!

干杯.克里斯

推荐答案

你需要给一个或多个列和行权重".通常,您将恰好有一行和一列,权重为 1(默认值为零).当调整包含的小部件或窗口的大小时,任何具有权重的行或列都将扩展和收缩.

You need to give one or more columns and rows "weight". Typically you'll have exactly one row and one column with a weight of 1 (the default is zero). Any row or column with a weight will expand and contract when the containing widget or window is resized.

如果多于一行或多于一列具有权重,则权重描述了它们展开的比例.例如,如果一列的权重为 2,另一列的权重为 1,则具有 2 的列将扩展为具有 1 的列的两倍.

If more than one row or more than one column has a weight, the weight describes the proportion in which they expand. For example, if one column has a weight of 2 and another a weight of 1, the one with two will expand twice as much as the one with 1.

您可以使用 grid_rowconfiguregrid_columnconfigure 选项为包含的小部件分配权重:

You can assign weight using the grid_rowconfigure and grid_columnconfigure options to the containing widget:

f3.grid_rowconfigure(0, weight=1)
f3.grid_columnconfigure(0, weight=1)

有关权重的简要说明,请参阅标题为处理调整大小的部分在 tkdocs.com 网站上的网格教程中.

For a nice brief description of weights, see the section titled Handling Resize on the grid tutorial on the tkdocs.com website.

这篇关于TKinter - 使用网格布局的小部件不会“粘在"框架中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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