C语言学习网

Python中tkinter有哪些布局

发表于:2022-10-04 作者:安全数据网编辑
编辑最后更新 2022年10月04日,这篇文章给大家分享的是有关Python中tkinter有哪些布局的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、pack布局举例#pack布局案例import tkint

这篇文章给大家分享的是有关Python中tkinter有哪些布局的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、pack布局举例

#pack布局案例import tkinterbaseFrame = 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.

感谢各位的阅读!关于"Python中tkinter有哪些布局"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

0