当前位置 : 主页 > 编程语言 > python >

Python tkinter三种布局实例详解

来源:互联网 收集:自由互联 发布时间:2021-04-09
这篇文章主要介绍了Python tkinter三种布局实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一、pack布局举例 #pack布局

这篇文章主要介绍了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()

二、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()

三、place布局

1.明确方位的摆放

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

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

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

四、源码

D61_ThreeFrameOfTkinter.py

https://github.com/ruigege66/Python_learning/blob/master/D61_ThreeFrameOfTkinter.py​

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

网友评论