python tkinter实例_Python tkinter三种布局实例详解

这篇文章主要介绍了Python tkinter三种布局实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、pack布局举例

#

pack布局案例

import tkinter

baseFrame = tkinter.Tk()

# 以下代码都是创建一个组件, 然后布局

btn1 = tkinter.Button(baseFrame, text =

"A")

btn1.pack(side = tkinter.LEFT, expand =

tkinter.YES, fill = tkinter.Y)

btn2 = tkinter.Button(baseFrame, text =

"B")

btn2.pack(side = tkinter.TOP, expand =

tkinter.YES, fill = tkinter.BOTH)

btn2 = tkinter.Button(baseFrame, text =

"C")

btn2.pack(side = tkinter.RIGHT, expand =

tkinter.YES, fill = tkinter.NONE)

btn2 = tkinter.Button(baseFrame, text =

"D")

btn2.pack(side = tkinter.LEFT, expand =

tkinter.NO, fill = tkinter.Y)

btn2 = tkinter.Button(baseFrame, text =

"F")

btn2.pack(side = tkinter.BOTTOM, expand =

tkinter.YES)

btn2 = tkinter.Button(baseFrame, text =

"G")

btn2.pack(anchor = tkinter.SE)

baseFrame.mainloop()

202016105653931.jpg?202006105710

二、grid布局举例

1.通用使用方式:组件对象,grid(设置.......)

2.利用row,column编号,都是从0开始

3.sticky:N\E\S\W表示上下左右,用来决定组件从哪个方向开始

4.支持ipadx,padx等参数,跟pack函数含义一样

5.支持rowspan,columnspan,表示跨行,跨列数量

#

pack布局案例

import tkinter

# baseFrame = tkinter.Tk()

## 以下代码都是创建一个组件, 然后布局

# btn1 = tkinter.Button(baseFrame, text =

"A")

# btn1.pack(side = tkinter.LEFT, expand =

tkinter.YES, fill = tkinter.Y)

#

# btn2 = tkinter.Button(baseFrame, text =

"B")

# btn2.pack(side = tkinter.TOP, expand =

tkinter.YES, fill = tkinter.BOTH)

#

# btn2 = tkinter.Button(baseFrame, text =

"C")

# btn2.pack(side = tkinter.RIGHT,

expand = tkinter.YES, fill = tkinter.NONE

)

#

# btn2 = tkinter.Button(baseFrame, text =

"D")

# btn2.pack(side = tkinter.LEFT, expand =

tkinter.NO, fill = tkinter.Y)

#

# btn2 = tkinter.Button(baseFrame, text =

"F")

# btn2.pack(side = tkinter.BOTTOM,

expand = tkinter.YES)

#

# btn2 = tkinter.Button(baseFrame, text =

"G")

# btn2.pack(anchor = tkinter.SE)

#

#

# baseFrame.mainloop()

baseFrame2 = tkinter.Tk()

lb1 = tkinter.Label(baseFrame2, text =

"账号:")

lb1.grid(row = 0, sticky = tkinter.W)

tkinter.Entry(baseFrame2).grid(row = 0,

column = 1, sticky = tkinter.E)

lb2 = tkinter.Label(baseFrame2, text =

"密码:")

lb2.grid(row = 1, sticky = tkinter.W)

tkinter.Entry(baseFrame2).grid(row = 1,

column = 1, sticky = tkinter.E)

button = tkinter.Button(baseFrame2,

text = "登录").grid(row = 2, column = 1,

sticky = tkinter.E)

baseFrame2.mainloop()

202016105653932.jpg?202006105710

三、place布局

1.明确方位的摆放

2.相对位置布局,随意改变窗口大小会导致混乱。

3.使用place函数,分为绝对布局和相对布局,绝对布局使用x,y参数

4.相对布局使用relx.rely,relheight,relwidth.

四、源码

D61_ThreeFrameOfTkinter.py

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值