您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章给大家分享的是有关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.
感谢各位的阅读!关于“Python中tkinter有哪些布局”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。