pack布局非常簡單,不用做過多的設置,直接使用一個 pack 函數就可以了。
1、我們使用 pack 函數的時候,默認先使用的放到上面,然 后 依次向下排,它會給我們的組件一個自認為合適的位置 和大小,這是默認方式。
2、可接受的參數:
side:按扭停靠在窗口的哪個位置
left: 左
top: 上
right: 右
botton: 下
fill:填充
x:水平方向填充
y:豎直方向填充
both:水平和豎直方向填充
none:不填充
expand:
yes:擴展整個空白區
no:不擴展
anchor:
N:北 下
E:東 右
S:南 下
W:西 左
CENTER:中間
padx:x方向的外邊距
pady:y方向的外邊距
ipadx:x方向的內邊距
ipady:y方向的內邊距
示例代碼:
#!/usr/bin/env python # _*_ coding:utf-8 _*_ from Tkinter import * root = Tk() Button(root,text='A').pack(side=LEFT,expand=YES,fill=Y) Button(root,text='B').pack(side=TOP,expand=YES,fill=BOTH) Button(root,text='C').pack(side=RIGHT,expand=YES,fill=NONE) Button(root,text='D').pack(side=LEFT,expand=NO,fill=Y) Button(root,text='E').pack(side=TOP,expand=YES,fill=BOTH) Button(root,text='F').pack(side=BOTTOM,expand=YES) Button(root,text='G').pack(anchor=SE) root.mainloop()
效果圖: