简体   繁体   English

TKinter - 使用网格布局的小部件不会“粘在”框架中

[英]TKinter - widgets not 'sticking' in Frame using grid layout

I am building a fairly complicated GUI in TKinter so naturally have been using the .grid function.我正在 TKinter 中构建一个相当复杂的 GUI,所以很自然地一直在使用 .grid 函数。

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

Example:例子:

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

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".在上面的代码中,框架f3填充了我的根小部件的 0,0 单元格中的空间,但列表框并没有填充框架中的所有空间,即使我已经要求它是粘性的“NS”。

If put the Listbox in the root widget at 0,0 then it stretches and fills all the space fine.如果将列表框放在根小部件中的 0,0 处,则它会拉伸并填充所有空间。 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!!!我认为使用框架会简化我的布局,但事实并非如此!!!

Cheers.干杯。 Chris克里斯

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).通常,您将恰好有一行和一列,权重为 1(默认值为零)。 Any row or column with a weight will expand and contract when the containing widget or window is resized.当调整包含的小部件或窗口的大小时,任何具有权重的行或列都将扩展和收缩。

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.例如,如果一列的权重为 2,另一列的权重为 1,则具有 2 的列将扩展为具有 1 的列的两倍。

You can assign weight using the grid_rowconfigure and grid_columnconfigure options to the containing widget:您可以使用grid_rowconfiguregrid_columnconfigure选项为包含的小部件分配权重:

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

For a nice brief description of weights, see the section titled Handling Resize on the grid tutorial on the tkdocs.com website.有关权重的简要说明,请参阅tkdocs.com网站上网格教程中标题为“处理调整大小”的部分。

First of all you shouldn't grid the lable at the same time when you instantiate it.首先,您不应该在实例化标签的同时对其进行网格化。 Then, you have to use Grid.rowconfigure to add 'weight' to the row and Grid.columnconfigure to add it to the column.然后,您必须使用 Grid.rowconfigure 将“权重”添加到行,并使用 Grid.columnconfigure 将其添加到列。 The weight controls expansion and has to be non-zero to allow grid to expand widgets.权重控制扩展并且必须为非零以允许网格扩展小部件。 If you add the following lines to your code it should work:如果您将以下几行添加到您的代码中,它应该可以工作:

Grid.columnconfigure(f3, 0, weight=1)
Grid.rowconfigure(f3, 0, weight=1)
Grid.rowconfigure(f3, 1, weight=1)

Hope it helps希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM