Tkinter pack常用选项及功能 side 设置组件的添加位置,可以设置为 TOP、BOTTOM、LEFT 或 RIGHT。 expand 该 bool 值指定当父容器增大时是否拉伸组件所占位置大小。 fill 当expand=1,设置组件是否填充组件变化后的位置。该选项支持 NONE、X、Y、BOTH 四个值,其中 NONE 表示不填充, BOTH 表示沿着两个方向填充。
expand、fill和side是相互影响的,fill和expand取决于父容器的位置。anchor 控件对齐方式:N\S\W\E\NW\SW\SE\NE 默认CENTER。 通用属性: ipadx 指定组件在 x 方向(水平)上的内部留白。按钮的边框与文字距离 ipady 指定组件在 y 方向(水平)上的内部留白。 padx 指定组件在 x 方向(水平)上与其他组件的间距。两个按钮之间距离 pady 指定组件在 y 方向(水平)上与其他组件的间距。
from tkinter import *
root = Tk()
root.title('pack布局')
root.geometry("250x250+30+30")
#side和fill的使用
fm1 = Frame(root)
fm1.pack(side=TOP,fill=X)
fm2 = Frame(root)
fm2.pack(side=TOP,fill=X)
fm3 = Frame(root)
fm3.pack(side=LEFT,fill=Y)
fm4 = Frame(root)
fm4.pack(side=LEFT,fill=Y)
fm5 = Frame(root)
fm5.pack(side=LEFT,fill=Y)
#fill 和 expand区别
Button(fm1, text='按钮1').pack(side=LEFT,expand=1)
Button(fm1, text='按钮2').pack(side=LEFT,fill=X, expand=1)
Button(fm1, text='按钮3').pack(side=LEFT,fill=X, expand=1)
Button(fm2, text='按钮1').pack(side=LEFT,fill=X)
Button(fm2, text='按钮2').pack(side=LEFT,fill=X, expand=1)
Button(fm2, text='按钮3').pack(side=LEFT,fill=X, expand=1)
#在父容器side确定位置后,控件side对fill的影响
Button(fm3, text='按钮1').pack(side=TOP, fill=Y, expand=YES)
Button(fm3, text='按钮2').pack(side=TOP, fill=Y, expand=YES)
Button(fm3, text='按钮3').pack(side=TOP, fill=Y, expand=YES)
Button(fm4, text='按钮1').pack(side=LEFT, fill=Y, expand=YES)
Button(fm4, text='按钮2').pack(side=LEFT, fill=Y, expand=YES)
Button(fm4, text='按钮3').pack(side=LEFT, fill=Y, expand=YES)
#anchor的使用
size=['da','xiao','zhong']
for each in size :
cb=Checkbutton(fm5,text=each)
cb.pack(anchor=E) #左对齐
root.mainloop()
来源:CSDN
作者:flyingc
链接:https://blog.csdn.net/u011878611/article/details/103836505