如何使用网格几何管理器将 Tkinter 中 LabelFrame 中的所有行和列居中?默认情况下,所有内容都在其单元格中居中,但是如果 LabelFrame 具有设置的高度和宽度,我如何将单元格居中?
例如:
self.labelframe = Labelframe(self, text="First Labelframe",height=200, width=200)
self.labelframe.pack()
self.label1 = Label(self.labelframe, text="Label1")
self.label1.grid(row=0, column=0)
self.label2 = Label(self.labelframe, text="Label2")
self.label2.grid(row=0, column=1)
self.label3 = Label(self.labelframe, text="Label3")
self.label3.grid(row=1, column=0)
在此示例中,标签在其单元格中居中,但单元格将从 LabelFrame 的左上角开始。有没有办法让单元格居中?
谢谢!
一个简单的解决方案是在顶部和底部添加一个空行,在左侧和右侧添加一个空列。给这些行和列一个权重,以便为它们提供任何额外的空间。
self.label1.grid(row=1, column=1)
self.label2.grid(row=1, column=2)
self.label3.grid(row=2, column=1)
self.labelframe.grid_rowconfigure(0, weight=1)
self.labelframe.grid_rowconfigure(3, weight=1)
self.labelframe.grid_columnconfigure(0, weight=1)
self.labelframe.grid_columnconfigure(3, weight=1)
另一种解决方案是添加另一个围绕self.labelframe
. 然后,您可以self.labelframe
保持它想要的大小,但让它在另一个框架中居中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句