今天制作了一个简单的基于网格的Tkinter Frame
子类。它的主人是Tk()
对象。
root = Tk()
root.title('Rep Logger UI')
root.geometry('500x500')
mother = rep_ui(master=root)
mother.mainloop()
我想做的就是将<{1}}窗口划分为两个(红色,蓝色)等宽的框架,然后放入几个小部件每个
但我没有做对。红色+蓝色框架没有完全填满窗户。要么他们没有完全填充rep_ui
类,要么rep_ui
类没有在其主文件(rep_ui
)内正确扩展?
此外,蓝色和红色框架具有不同的 root
,即使我使用相同的 width
-s对其进行列配置
weight
以下是我所描述的问题的图片:
答案 0 :(得分:3)
为什么这些窗口宽度不同?
原因很明显:红框内有一个文本小部件。问题是您将weight
参数添加到两个框架中,但未添加到内部的 中。对于文本,你可以使用像
self.fl.rowconfigure(0, weight=1)
self.fl.columnconfigure(0, weight=1)
为什么会出现这个空白区域?
那是因为你在这里配置了第1行:
self.rowconfigure(1, weight=1)
虽然没有使用,但它会出现。删除或评论此行以使其正常工作。
答案 1 :(得分:1)
weight
参数仅影响额外空间的分发方式。即网格管理员首先对所有&#34;孙子&#34;窗口小部件,如果有额外的空间,则根据提供的权重将其分发给子窗口小部件。 (documented here:权重参数&#34;给出分配额外空格时此列或行的相对权重&#34;)
视觉解释:
至于底部的空白区域,我认为我们需要看到剩下的代码来解决这个问题。
答案 2 :(得分:-2)
place
几何管理器:self.fl.place( relx = 0.0, rely = 0.0, relwidth = 0.5, relheight = 1.0 )
self.fr.place( relx = 0.5, rely = 0.0, relwidth = 0.5, relheight = 1.0 )
使用父窗口小部件相对比例相当强大,但是对于重新缩放UI仍然很容易编码和验证以及面向未来,并且不会受到其他工件和非预期的副作用的影响,复杂的UI设计可能会在其中遇到生命周期。
root = Tkinter.Tk()
aLF = Tkinter.Frame( root, bg = "red" )
aRF = Tkinter.Frame( root, bg = "blue")
aLF.place( relx = 0.0,
rely = 0.0, relwidth = 0.5, relheight = 1.0 )
aRF.place( relx = 0.5,
rely = 0.0, relwidth = 0.5, relheight = 1.0 )
aTextR = Tkinter.Label( aRF, bg = "white", text = "aFR := frame right" )
aTextR.place( relx = 0.0,
rely = 0.0 )
aTextL = Tkinter.Label( aLF, bg = "white", text = "aFL := frame left" )
aTextL.place( relx = 0.0,
rely = 0.0 )
root.title( 'Rep Logger UI')
root.geometry( '500x500' )
root.lift()
并交叉验证
root.geometry( '500x500' )
root.geometry( '300x100' )
root.geometry( '400x300' )
root.geometry( '600x150' )