python之tkinter库的pack布局讲解

pack布局是tkinter三种布局中的一种。简单的界面可以直接使用pack方式,复杂的需要结合frame实现。

pack()方法的参数:side,fill,padx,pady,ipadx,ipady,anchor,expand。

参数

属性

举例

side

组件停靠方向

left

right

top:默认值

bottom

l.pack(side=’top’) 向上停靠

fill

组件是否填充及填充方向。

横向填充 x

纵向填充 y

不填充 none

横纵都填充 both

fill = “both”

padx,pady

组件外,和组件临近的组件或窗体边界的距离。(外边距)

默认值:0

padx=(5,10) 组件距左边的组件或窗体边缘5个像素,右边10个像素。

la1.pack(padx=(0,100),pady=(50,0)) 

ipadx,ipady

组件内,组件文本跟组件边界之间的距离。(内边距)

默认值:0

anchor

组件停靠的位置。

center:居中显示(默认值)。

n:上

s:下

w:左

e:右

nw:左上

ne:右上

sw:左下

se:右下

expand

组件的扩展范围。

true:

false:不扩展,组件只在自己的范围内。

综合示例:

from tkinter import *
root=Tk()
root.geometry('800x450+300+200')
lsidetest=Label(root,text='sidetest',bg='blue')
lsidetest.pack(side='right')  # 靠右,默认是top
# lsidetest.pack(side='nw') #错误示例,side无此属性,anchol才有
lfilltest=Label(root,text='filltest',bg='yellow')
# lfilltest.pack(side='top',fill='x')  #靠上 横向填充
# lfilltest.pack(side='top',fill='y')  #靠上、下时,不能纵向填充
# lfilltest.pack(side='top',fill='y',expand='true')  #靠上、下时,不能纵向填充,但是expand为true可以。
lfilltest.pack(side='left',fill='y')  #靠左、右时,不能横向,只能纵向填充,但是expand为true时可以。
lancholtest=Label(root,text='ancholtest',bg='green')
# lancholtest.pack(anchor='ne')  # 靠右上
# lancholtest.pack(side='bottom',anchor='ne')  # 根据anchor是右上,但side是bottom,先按照bottom再按照anchor,定位于右下
lancholtest.pack(anchor='ne',side='bottom')  # 根据anchor是右上,但side是bottom
lpadtest=Label(root,text='padtest',bg='red')
lpadtest.pack(padx=(100,200),pady=(50,300)) #  距左边和右边的距离;距上边和下边的距离
lipadtest=Label(root,text='ipadtest',bg='orange')
lipadtest.pack(ipadx=30,ipady=20)

root.mainloop()

  • 18
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值