我正在尝试向使用 python 3.x 的 tkinter 应用程序中创建的所有小部件添加非零权重值。我想让所有子框架中的所有网格都随着根窗口展开。
我有这个代码:
# Gets the size of the roots grid
Grid_Size = root.grid_size()
# Loops through the columns in the grid
for i in range(Grid_Size[0]):
# Configures a weight of 1 to the column
root.columnconfigure(i, weight=1)
# Loops through all the rows in the grid
for i in range(Grid_Size[1]):
# Configures a weight of 1 to the row
root.rowconfigure(i, weight=1)
但是,这仅适用于根第一网格,而不适用于任何 cildrens 网格。
我的下一次尝试尝试在根目录中查找所有项目,然后单独配置所有这些项目:
# Creats a list of the frames children
child_list = root.winfo_children()
# Iterates though every child in the list
for child in child_list:
# If the child has children
if child.winfo_children():
# Adds the children to the list to be iterated though
child_list.extend(child.winfo_children())
# Goes through all of the widgets
for child in child_list:
# Gets the nuber of rows and columns of the grid
Grid_Size = child.grid_size()
# Loops through every column
for i in range(Grid_Size[0]):
# Sets the weight to a non zero value so it can expand
child.columnconfigure(i, weight=1)
# Loops through every row
for i in range(Grid_Size[1]):
# Sets the weight to a non zero value so it can expand
child.rowconfigure(i, weight=1)
但是,这个simplay似乎不起作用。正在调用行和列配置方法,但小部件仍未随根窗口展开。所有框架和小部件都放置在根目录下
小部件.grid(行=x,列=y,粘性=“nsew”)
其中 x 和 y 是相关数字