在LabelFrame中居中项目

时间:2013-08-06 12:30:19

标签: python python-2.7 tkinter

enter image description here

我希望将此LabelFrame中的所有项目集中在一起。它填充这个广泛的原因是下面的其他小部件比最初的LabelFrame宽。 NewCustomerFrameLabelFrame个对象。

#notebook
self.nb = Notebook(self, name='notebook')

#admin frame
admin_frame = Frame(self.nb, name='admin')
#new customer
nc_frame = NewCustomerFrame(admin_frame, self.customers)
nc_frame.pack(padx=5,pady=5,ipadx=30,ipady=5,fill='x')
''' bla bla bla '''
admin_frame.pack()
self.nb.add(admin_frame, text="Admin",sticky='ew')

1 个答案:

答案 0 :(得分:0)

如果您希望其他人帮助您,您真的需要提供最小的工作演示!

在我看来,使用Tkinter网格设计器时更容易处理居中元素(参见when using which layout manager)。您只需将元素放在矩阵上即可。网格布局的主要缺点是难以调整元素大小(没有填充选项)。我们必须在行和列上放置权重(请参阅grid_rowconfiguregrid_columnconfigure)并将控件放在每个边缘(NSWE)。

在完成代码之前,警告:不要在同一根框架中混合网格和包装!在我的例子中,我使用pack来堆叠标签,并使用网格来管理标签本身内的元素。我用this library for the Notebook class

root = Tk()


#notebook
nb = notebook(root )


# Admin Tab
admin = Frame( nb() , name='admin') 
admin.grid( sticky=N+S+W+E)

# Every row and column have a wieght of one when resizing (linear transformation)
admin.grid_rowconfigure(0, weight=1)
admin.grid_rowconfigure(1, weight=1)
admin.grid_rowconfigure(2, weight=1)

admin.grid_columnconfigure(1, weight=1)
admin.grid_columnconfigure(2, weight=1)
admin.grid_columnconfigure(3, weight=1)

new_customer = Frame( admin , relief = RIDGE )

# Labels init
Label(admin, text="Name").grid(row=0, column = 0, sticky=N+S+W+E)
Label(admin, text="Type").grid(row=1, column = 0, sticky=N+S+W+E)
Label(admin, text="Date").grid(row=1, column = 2, sticky=N+S+W+E)

 # Elements Init
first_name  = Entry(admin)
middle_name = Entry(admin)
family_name     = Entry(admin)
date        = Entry(admin)

type_box = OptionMenu( admin , "one", "two", "three")


reset_button = Button( admin, text = "Reset Value" )
submit_button = Button( admin, text = "Submit"  )


# Place elements    
first_name.     grid(row=0  , column  =1, sticky=N+S+W+E)
middle_name.    grid(row=0  , column  =2, sticky=N+S+W+E)
family_name.    grid(row=0  , column  =3, sticky=N+S+W+E)
date.       grid(row =1 , column  =3,  sticky=N+S+W+E)
type_box.   grid(row =1  , column  =1, sticky=N+S+W+E)
reset_button.   grid(row =2 , column  = 1 ,sticky=N+S+W+E)
submit_button.  grid(row =2 , column  = 3 , sticky=N+S+W+E)



# Add Tabls
nb.add_screen( admin , "Admin" )
nb.add_screen( Frame(nb()) , "Others" )



if __name__ == "__main__":
        root.mainloop() 

该应用程序并不完美(没有填充,行没有扩展),但它应该让你先行一步。