简体   繁体   English

如何使窗口自动调整大小?

[英]How to make the window resize automatically?

I am tried to see different sources that speak about dynamic resize of the window but nothing is really working with me.我试图查看有关窗口动态调整大小的不同来源,但没有任何内容对我有用。 I tried the following code but as you will see in the figures below more than half of the window is empty when maximizing the window.我尝试了以下代码,但正如您将在下图中看到的那样,最大化窗口时,超过一半的窗口是空的。

root.geometry('500x600')
root.title('Optimization Sphere')
root.resizable(True, True)

在此处输入图片说明 在此处输入图片说明

My guess is that you're using grid exclusively, or at least for the layout of the highest level of widgets.我的猜测是您专门使用grid ,或者至少用于最高级别小部件的布局。

With grid , rows and columns will expand or shrink to fit their contents as best they can.使用grid ,行和列将尽可能地扩展或收缩以适应其内容。 If there is extra space, that extra space will go unused.如果有额外空间,该额外空间将被闲置。

You can configure grid to allocate space to one or more rows and one or more columns.您可以配置grid为一行或多行和一或多列分配空间。 As a rule of thumb when using grid , you should always do this for at least one row and at least one column.根据经验,使用grid ,您应该始终至少对一行和至少一列执行此操作。 Typically that is the column with the largest widget -- a text widget, a canvas, a tree, something like that.通常,这是具有最大小部件的列——文本小部件、画布、树等。

You control this by giving a row or column 'weight'.您可以通过给一行或一列“权重”来控制它。 The weight tells grid what to do with extra space.权重告诉grid如何处理额外的空间。 The values are relative, so if you have one column with a weight of 1 and one with a weight of 3, the one with the weight of 3 will get three out of every four extra pixels.这些值是相对的,因此如果您有一个权重为 1 的列和一个权重为 3 的列,那么权重为 3 的列将从每四个额外像素中获得三个。

In your case I'm guessing you want to give all extra space to column 1, which you would do like this:在您的情况下,我猜您想为第 1 列提供所有额外空间,您可以这样做:

root.grid_columnconfigure(1, weight=1)

Without seeing your actual code it's impossible to know if that's the exact right thing to do.如果没有看到您的实际代码,就不可能知道这是否是正确的做法。 However, the point remains that if you're using grid , you need to give a non-zero weight to at least one column if you want that column to expand to fill empty space.但是,重点仍然是,如果您使用grid ,如果您希望该列扩展以填充空白空间,则需要为至少一列赋予非零权重。

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

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