当前位置:   article > 正文

Python TKinter 布局管理 (Pack Place Grid)_python pack grid place 混用

python pack grid place 混用

Tkinter是Python常用的一个GUI库,本文主要介绍了Tkinter的布局管理部分。

Tkinter有三种布局管理方式:

  • pack
  • grid
  • place

这三种布局管理在同一个 master window 里是不可以混用的。

pack布局管理

pack布局非常简单,不用做过多的设置,直接使用一个 pack 函数就可以了。

1、我们使用 pack 函数的时候,默认先使用的放到上面,然 后依次向下排,它会给我们的组件一个自认为合适的位置和大小,这是默认方式。

2、可接受的参数:

  side:停靠在哪个方向

    left: 左

    top: 上

    right: 右

    botton: 下

  fill:填充

    x:水平方向填充

    y:竖直方向填充

    both:水平和竖直方向填充

    none:不填充

  expand:

    True:随主窗体的大小变化

    False:不随主窗体的大小变化

  anchor:

    N:北  下

    E:东  右

    S:南 下

    W:西 左

    CENTER:中间

  padx:x方向的外边距

  pady:y方向的外边距

  ipadx:x方向的内边距

  ipady:y方向的内边距

示例代码:

  1. #!/usr/bin/env python
  2. # _*_ coding:utf-8 _*_
  3. from tkinter import *
  4. root = Tk()
  5. Button(root,text='A').pack(side=LEFT,expand=YES,fill=Y)
  6. Button(root,text='B').pack(side=TOP,expand=YES,fill=BOTH)
  7. Button(root,text='C').pack(side=RIGHT,expand=YES,fill=NONE)
  8. Button(root,text='D').pack(side=LEFT,expand=NO,fill=Y)
  9. Button(root,text='E').pack(side=TOP,expand=YES,fill=BOTH)
  10. Button(root,text='F').pack(side=BOTTOM,expand=YES)
  11. Button(root,text='G').pack(anchor=SE)
  12. root.mainloop()

参考博文:https://blog.csdn.net/liuxu0703/article/details/54428405

Grid 布局管理

Pack 作为首选的布局管理方式,其运作方式并不是特别易于理解,已经由 Pack 布局完成的设计也很难做出改变。Grid 布局在1996年作为另一种可供选择的布局方式被引入,Grid 布局方式易学易用,但似乎大家还是习惯用 Pack。

Grid 在很多场景下是最好用的布局方式,相比而言,Pack 布局在控制细节方面有些力不从心。Place 布局虽然可以完全控制控件位置,但这也导致使用 Place 会比其他两种布局方式更加复杂。

Grid 把控件位置作为一个二维表结构来维护,即按照行列的方式排列控件:控件位置由其所在的行号和列号决定。行号相同而列号不同的几个控件会被彼此上下排列,列号相同而行号不同的几个控件会被彼此左右排列。

使用 Grid 布局的过程就是为各个控件指定行号和列号的过程,不需要为每个格子指定大小,Grid 布局会自动设置一个合适的大小。

参考博文:https://www.cnblogs.com/ruo-li-suo-yi/p/7425307.html

Place 布局管理

Place 布局管理可以显式的指定控件的绝对位置或相对于其他控件的位置。要使用 Place 布局,调用相应控件的 place() 方法就可以了。所有 tkinter 的标准控件都可以调用 place()。

  1. '''Tkinter教程之Place篇'''
  2. '''1.使用绝对坐标将组件放到指定的位置'''
  3. # -*- coding: utf-8 -*-
  4. # 不设置root的大小,使用默认
  5. from tkinter import *
  6. root = Tk()
  7. lb = Label(root, text='hello Place')
  8. # lb.place(relx = 1,rely = 0.5,anchor = CENTER)
  9. # 使用绝对坐标将Label放置到(0,0)位置上
  10. lb.place(x=0, y=0, anchor=NW)
  11. root.mainloop()
  12. # x,y指定组件放置的绝对位置
  1. '''2.使用相对坐标放置组件位置'''
  2. # -*- coding: utf-8 -*-
  3. # 不设置root的大小,使用默认
  4. from tkinter import *
  5. root = Tk()
  6. lb = Label(root, text='hello Place')
  7. # lb.place(relx = 1,rely = 0.5,anchor = CENTER)
  8. # 使用相对坐标(0.5,0.5)将Label放置到(0.5*sx,0.5.sy)位置上
  9. lb.place(relx=0.5, rely=0.5, anchor=CENTER)
  10. root.mainloop()
  11. # relx,rely指定组件放置的绝对位置,范围为(0-1.0)
  1. '''3.使用place同时指定多个组件'''
  2. # -*- coding: utf-8 -*-
  3. # 不设置root的大小,使用默认
  4. from tkinter import *
  5. root = Tk()
  6. root.geometry('800x600')
  7. lb = Label(root, text='hello Place')
  8. # lb.place(relx = 1,rely = 0.5,anchor = CENTER)
  9. # 使用相对坐标(0.5,0.5)将Label放置到(0.5*sx,0.5.sy)位置上
  10. v = IntVar()
  11. for i in range(5):
  12. Radiobutton(
  13. root,
  14. text='Radio' + str(i),
  15. variable=v,
  16. value=i
  17. ).place(x=80 * i, anchor=NW)
  18. root.mainloop()
  19. # 使用place来指定各个Radiobutton的位置
  1. '''4.同时使用相对和绝对坐标'''
  2. # 同时设置relx,rely和x,y的值
  3. # -*- coding: utf-8 -*-
  4. # 不设置root的大小,使用默认
  5. from tkinter import *
  6. root = Tk()
  7. root.geometry('800x600')
  8. lb1 = Label(root, text='hello Place', fg='green')
  9. lb2 = Label(root, text='hello Place', fg='red')
  10. # 先设置相对坐标为(0.5,0.5),再使用(-200,-200)将坐标作偏移(-200,-200)
  11. lb1.place(relx=0.5, rely=0.5, anchor=CENTER, x=-200, y=-200)
  12. # 先设置相对坐标为(0.5,0.5),再使用(-300,-300)将坐标作偏移(-300,-300)
  13. lb2.place(relx=0.5, rely=0.5, anchor=CENTER, x=-300, y=-300)
  14. root.mainloop()
  15. # 同时使用相对和绝对坐标时,相对坐标优先操作,然后是在这个相对坐标的基础上进行偏移
  1. '''5.使用in来指定放置的容器'''
  2. # -*- coding: utf-8 -*-
  3. # 使用in属性来指定放置到的容器是那一个
  4. from tkinter import *
  5. root = Tk()
  6. root.geometry('800x600')
  7. lb1 = Label(root, text='hello Place Label', fg='green')
  8. bt1 = Button(root, text='hello Place Button', fg='red')
  9. # 创建一个Label
  10. lb1.place(relx=0.5, rely=0.5, anchor=CENTER)
  11. # 在root同创建一个Button,目的是与bt1相比较
  12. bt2 = Button(root, text='button in root', fg='yellow')
  13. bt2.place(anchor=W)
  14. # 在Label中创建一个Button
  15. bt1.place(in_=lb1, anchor=W)
  16. root.mainloop()
  17. # 注意bt2放置的位置是在root的(0,0)处,而button1放置的位置是在lb1的(0,0)处,原因是由于bt1使用了in来指定放置的窗口为lb1
  1. '''6.深入in用法'''
  2. # -*- coding: utf-8 -*-
  3. # 使用in属性来指定放置到的容器是那一个,仅能是其master
  4. from tkinter import *
  5. root = Tk()
  6. # root.geometry('800x600')
  7. # 创建两个Frame用作容器
  8. fm1 = Frame(root, bg='red', width=40, height=40)
  9. fm2 = Frame(root, bg='blue', width=40, height=40)
  10. # 再在fm1中创建一个fm3
  11. fm3 = Frame(fm1, bg='yellow', width=20, height=20)
  12. # 创建一个Label,它的master为fm1
  13. lb1 = Label(fm1, text='hello Place', fg='green')
  14. lb1.place(in_=fm1, relx=0.5, rely=0.5, anchor=CENTER)
  15. # 创建一个Button,它的master为fm1
  16. bt1 = Button(fm1, text='hello Place', fg='red')
  17. # 将bt1放置到fm2中,程序报错
  18. # 去掉下面这条语句就可以使用了,可以看到lb1已经正确的放置到fm1的中心位置了
  19. # bt1.place(in_ = fm2,anchor = W)
  20. # 将上面的语句改为下面,即将bt1放置到其fm1的子组件fm3中,这样也是可以的
  21. bt1.place(in_=fm3, anchor=W)
  22. fm1.pack()
  23. fm2.pack()
  24. fm3.pack()
  25. root.mainloop()
  26. # in不是可以随意指定放置的组件的,如果使用in这个参数这个组件必需满足:是其父容器或父容器的子组件
  1. '''7.事件与Place结合使用'''
  2. # -*- coding: utf-8 -*-
  3. # 最后使用两个place方法来动态改变两个Frame的大小。
  4. from tkinter import *
  5. root = Tk()
  6. split = 0.5
  7. fm1 = Frame(root,bg = 'red')
  8. fm2 = Frame(root,bg = 'blue')
  9. # 单击fm1时增大它的占有区域0.1
  10. def incFm1(event):
  11. global split
  12. if split < 1:
  13. split += 0.1
  14. fm1.place(rely = 0,relheight = split,relwidth = 1)
  15. fm2.place(rely = split,relheight = 1 - split,relwidth = 1)
  16. # 单击fm2时增大它的占有区域0.1
  17. def incFm2(event):
  18. global split
  19. if split > 0:
  20. split -= 0.1
  21. fm1.place(rely = 0,relheight = split,relwidth = 1)
  22. fm2.place(rely = split,relheight = 1 - split,relwidth = 1)
  23. # 这两语句要使用,不然开始看不到两个frame,也就没法点击它们了
  24. fm1.place(rely = 0,relheight = split,relwidth = 1)
  25. fm2.place(rely = split,relheight = 1 - split,relwidth = 1)
  26. # 绑定单击事件
  27. fm1.bind('<Button-1>',incFm1)
  28. fm2.bind('<Button-1>',incFm2)
  29. root.mainloop()
  30. # 为SplitWindow的原型了,再改动一下就可以实现一个SplitWindow了。

参考博文:https://blog.csdn.net/aa1049372051/article/details/51887144

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/724902
推荐阅读
相关标签
  

闽ICP备14008679号