Python “重量”在 tkinter 中有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45847313/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:19:24  来源:igfitidea点击:

What does 'weight' do in tkinter?

pythontkinter

提问by Fred

I've been searching different websites trying to find out what weight does in tkinter. I got this from TkDocs:

我一直在搜索不同的网站,试图找出权重在 tkinter 中的作用。我从TkDocs得到了这个:

Every column and row has a "weight" grid option associated with it, which tells it how much it should grow if there is extra room in the master to fill. By default, the weight of each column or row is 0, meaning don't expand to fill space.

每列和每行都有一个与之关联的“权重”网格选项,它告诉它如果母版中有额外的空间来填充它应该增长多少。默认情况下,每列或每行的权重为 0,这意味着不扩展以填充空间。

Could someone please put this into some context for me, as I'm struggling to understand what it does. I've experimented with the following code, and it just seems to move things across the page as I change the values.

有人可以帮我把它放在一些上下文中,因为我正在努力理解它的作用。我已经尝试了以下代码,当我更改值时,它似乎只是在页面上移动内容。

try:
    import tkinter
except ImportError:  # python 2
    import Tkinter as tkinter

import os

mainWindow = tkinter.Tk()

mainWindow.title("Grid demo")
mainWindow.geometry('640x480-8-200')

label = tkinter.Label(mainWindow, text="tkinter grid demo")
label.grid(row=0, column=0, columnspan=3)

mainWindow.columnconfigure(0, weight=1)
mainWindow.columnconfigure(1, weight=1)
mainWindow.columnconfigure(2, weight=3)
mainWindow.columnconfigure(3, weight=3)
mainWindow.columnconfigure(4, weight=3)
mainWindow.rowconfigure(0, weight=1)
mainWindow.rowconfigure(1, weight=10)
mainWindow.rowconfigure(2, weight=1)
mainWindow.rowconfigure(3, weight=3)
mainWindow.rowconfigure(4, weight=3)

fileList = tkinter.Listbox(mainWindow)
fileList.grid(row=1, column=0, sticky='nsew', rowspan=2)
fileList.config(border=2, relief='sunken')

for zone in os.listdir('/Windows/System32'):
    fileList.insert(tkinter.END, zone)
mainWindow.mainloop()

回答by Bryan Oakley

In the simplest terms possible, a non-zero weight causes a row or column to grow if there's extra space. The default is a weight of zero, which means the column will not grow if there's extra space.

用最简单的术语来说,如果有额外的空间,非零权重会导致行或列增长。默认权重为零,这意味着如果有额外空间,列将不会增长。

Consider the following code, which creates a window larger than the widgets that are inside, and for which no columns have weight:

考虑下面的代码,它创建一个比里面的小部件更大的窗口,并且没有列有权重:

import tkinter as tk

root = tk.Tk()
root.geometry("200x100")

f1 = tk.Frame(root, background="bisque", width=10, height=100)
f2 = tk.Frame(root, background="pink", width=10, height=100)

f1.grid(row=0, column=0, sticky="nsew")
f2.grid(row=0, column=1, sticky="nsew")

root.grid_columnconfigure(0, weight=0)
root.grid_columnconfigure(1, weight=0)

root.mainloop()

This is what the window looks like:

这是窗口的样子:

window with no weights applied

没有应用权重的窗口

The reason it looks that way is because tkinter has been told not to give any of the columns any extra space, so the extra space goes unused to the right.

它看起来如此的原因是因为 tkinter 被告知不要给任何列任何额外的空间,所以额外的空间在右边没有使用。

Now, change the code so that we give a weight to just one column:

现在,更改代码,以便我们只为一列赋予权重:

root.grid_columnconfigure(0, weight=1)

When we restart, we now have a window that looks like this:

当我们重新启动时,我们现在有一个如下所示的窗口:

window with weight on first column only

仅在第一列上具有权重的窗口

What happened? Because column zero had a weight of one, tkinter gave the extra space to this column. You could have set the weight to 1, 100, 100000 and you would get the same result. In this case, all of the extra space goes to this one column.

发生了什么?由于第 0 列的权重为 1,因此 tkinter 为该列提供了额外的空间。您可以将权重设置为 1、100、100000,您将获得相同的结果。在这种情况下,所有额外的空间都进入这一列。

What happens if you give both columns a weight? The extra space is divided between the columns proportional to their weight. For example, let's say you want a navigation area on the left that takes up 1/4 of the screen and the main area should take up 3/4 of the screen (a 1:3 ratio).

如果你给两列一个权重会发生什么?额外的空间在与它们的重量成比例的列之间分配。例如,假设您希望左侧的导航区域占屏幕的 1/4,而主区域应占屏幕的 3/4(比例为 1:3)。

Let's change the weights to look like this:

让我们将权重更改为如下所示:

root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=3)

Since both columns have weight, extra space is given to both columns. For every four pixels of extra space, column 0 will get 1 and column 1 will get the other 3.

由于两列都有权重,因此两列都有额外的空间。对于每四个额外空间像素,第 0 列将获得 1,第 1 列将获得其他 3。

window with weight given to both columns

赋予两列权重的窗口

What's more, if you interactively resize the window, the proportion is preserved as much as possible. Here's the same window that I manually resized to be much wider:

更重要的是,如果您交互式地调整窗口大小,则会尽可能地保留比例。这是我手动调整大小以使其更宽的同一个窗口:

window after resizing

调整大小后的窗口