【问题标题】:Center a button widget in a column using tkinter使用 tkinter 在列中居中按钮小部件
【发布时间】:2018-09-13 13:31:38
【问题描述】:

我有一个标签框,在标签框内,我放置了一个按钮。该按钮将始终出现在 LabelFrame 的左上角,尽管我希望它在 LabelFrame 中居中。我缺少什么属性会强制此按钮在 LabelFrame 内居中?

self.f1_section_frame=LabelFrame(self.mass_window, text="LOCATIONS", width=300, height=998, padx=5, pady=5, bd=5)
self.f1_section_frame.grid(row=0, rowspan=6, column=1, sticky="nw", padx=(2,0))
self.f1_section_frame.grid_propagate(False)

self.button_frame1 = LabelFrame(self.f1_section_frame, width=275, height=50)
self.button_frame1.grid_propagate(False)
self.button_frame1.grid(row=1, column=0)
self.b1_scoring=Button(self.button_frame1, text="CONFIRM\nLOCATION(S)", height=2, width=10, command=self.initiate_site_scoring, justify="center")
self.b1_scoring.grid(row=0,column=0, pady=(1,0))

【问题讨论】:

  • 省略 pady 应该 - 根据documentation 将小部件居中在父级内。尽管如此,它只会在行/列内居中。根据它们的大小,它是否会出现居中。

标签: python button tkinter widget


【解决方案1】:

感谢@R4PH43L 的回复。我试了一下,它似乎没有改变。然而,这让我开始思考,所以我从包围我的按钮的框架中删除了“grid_propagate”,然后将框架包裹在按钮周围,没有任何空间,并将框架置于放置 IT 的列的中心。然后我在最左边的按钮上使用了 padx=(x,0) 并在最右边的按钮上使用了 padx=(0,x) 来添加左右两侧所需的空间,它现在正在按我需要的方式工作。

self.f1_section_frame=LabelFrame(self.mass_window, text="LOCATIONS", width=300, 
height=998, padx=5, pady=5, bd=5)

self.f1_section_frame.grid(row=0, rowspan=6, column=1, sticky="nw", padx=(2,0))
self.f1_section_frame.grid_propagate(False)

self.button_frame1 = LabelFrame(self.f1_section_frame, width=275, height=50)
self.button_frame1.grid(row=1, column=0)

self.b1_scoring=Button(self.button_frame1, text="CONFIRM\nLOCATION(S)", height=2, width=10, 
command=self.initiate_site_scoring, justify="center")

self.b1_scoring.grid(row=0,column=0, padx=(15,0))

self.b2_scoring=Button(self.button_frame1, text="CLEAR\nSELECTION(S)", height=2, 
width=10, command=self.clear_selected_locations)

self.b2_scoring.grid(row=0,column=1)

self.b3_scoring=Button(self.button_frame1, text="UPDATE\nSELECTION(S)", height=2, width=10, 
command=self.update_selected_location_details)

self.b3_scoring.grid(row=0,column=2, padx=(0,15))

【讨论】:

    猜你喜欢
    • 2022-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多