gpt4 book ai didi

python - 防止 Tkinter 网格动态调整单元格大小

转载 作者:行者123 更新时间:2023-12-01 07:14:30 25 4
gpt4 key购买 nike

我有一个带有网格的 Tkinter 类,每个网格行的权重应与行配置中所述的相同,并且当我关闭传播时,网格的内容不应影响网格尺寸。

只要网格中有带有背景颜色的空框架,这种方法就可以正常工作,但是一旦我将按钮添加到顶部网格行,该行的大小与其他行相比就会增加大约按钮的高度本身。代码如下:

class MainMenuPage(tkinter.Frame):
def __init__(self,parent,controller):
tkinter.Frame.__init__(self,parent)
self.controller=controller

self.columnconfigure(0,weight=1)
self.rowconfigure(0,weight=1)
self.rowconfigure(1,weight=1)
self.rowconfigure(2,weight=1)
self.rowconfigure(3,weight=1)
self.grid_propagate(0);

self.color0=tkinter.Frame(self,bg="#bd1e2b")
self.color0.grid(column=0,row=0,sticky="nesw")

self.color1=tkinter.Frame(self,bg="#1e1ebd")
self.color1.grid(column=0,row=1,sticky="nesw")

self.color2=tkinter.Frame(self,bg="#1ebd48")
self.color2.grid(column=0,row=2,sticky="nesw")

self.color3=tkinter.Frame(self,bg="#bdb81e")
self.color3.grid(column=0,row=3,sticky="nesw")

image=Image.open("/path/settingsIcon.png")
image=image.resize((54,54),Image.ANTIALIAS)
self.settingsIcon=ImageTk.PhotoImage(image)
self.settingsButton=tkinter.Button(self,compound=tkinter.TOP,width=54,height=54,image=self.settingsIcon,command=lambda:self.controller.showPage("valveMain"))
self.settingsButton.grid(column=0,row=0,sticky="ne")

enter image description here

添加按钮时如何防止网格改变大小?

最佳答案

我不完全理解你想要实现的目标。如果目标是确保每行和/或列的大小相同,您可以将行和列配置为统一组的一部分。仅 weight 无法做到这一点,因为它仅配置额外空间的分配方式,它不控制行或列的绝对大小。

要创建统一组,请使用 row_configurecolumn_configureuniform 选项。同一统一组中的所有行或列将具有与其权重成比例的相同大小。如果它们的重量相同,那么它们的尺寸也会相同。

来自规范的 tcl/tk 文档:

The -uniform option, when a non-empty value is supplied, places the row in a uniform group with other rows that have the same value for -uniform. The space for rows belonging to a uniform group is allocated so that their sizes are always in strict proportion to their -weight values.

根据您的情况,您可以尝试这样的操作:

self.rowconfigure(0,weight=1, uniform='row')
self.rowconfigure(1,weight=1, uniform='row')
self.rowconfigure(2,weight=1, uniform='row')
self.rowconfigure(3,weight=1, uniform='row')

有关更多信息,请参阅The grid algorithm在 tcl/tk 文档中。它准确地描述了网格算法的工作原理。

关于python - 防止 Tkinter 网格动态调整单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58042901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com