python

Tkinter自学02:tkinter 的3种布局管理器

2020-02-26  本文已影响0人  欠欠的小跟班

一、简介

所谓布局管理,就是对添加到窗口中的组件的大小和位置进行设置。此外,当用户调整了窗口大小后,布局管理器还会自动调整窗口中各个组件的大小和位置。
tkinter有三种布局管理器:
(1)Pack
(2)Gird
(3)Place

二、pack布局

使用pack布局时,当向窗口中添加组件时,这些组件会依次向后排列,排列方向可以是水平的,也可以是垂直的。

1.pack的参数

通常我推荐使用python自带的help()来查看我们不太熟悉的函数。

>>> help(tkinter.Label.pack)
Help on function pack_configure in module tkinter:

pack_configure(self, cnf={}, **kw)
    Pack a widget in the parent widget. Use as options:
    after=widget - pack it after you have packed widget
    anchor=NSEW (or subset) - position widget according to
                              given direction                                                                                     
    before=widget - pack it before you will pack widget                                                                           
    expand=bool - expand widget if parent size grows
    fill=NONE or X or Y or BOTH - fill widget if widget grows
    in=master - use master to contain this widget
    in_=master - see 'in' option description
    ipadx=amount - add internal padding in x direction
    ipady=amount - add internal padding in y direction
    padx=amount - add padding in x direction
    pady=amount - add padding in y direction
    side=TOP or BOTTOM or LEFT or RIGHT -  where to add this widget.

从上面可以看到,pack通常支持以下的方法:

方法名称 作用 可用的参数
anchor 根据给定方向放置位置小部件 N,W,S,E,NW,NE,SW,SE,CENTER(实际上是东南西北方位的简称,上北下南左西右东
expand 指定当父级容器增大时是否拉伸组件 布尔值,True,False
fill 设置组件是否延水平或垂直方向填充 X,Y,BOTH,NONE
ipadx 指定组件在x方向(水平方向)的内部留白 数值,表示像素
ipady 指定组件在y方向(垂直方向)的内部留白 数值,表示像素
padx 指定组件在x方向(水平方向)与其他组件的间距 数值,表示像素
pady 指定组件在y方向(垂直方向)与其他组件的间距 数值,表示像素
side 设置组件的添加位置 TOP,BOTTOM,LEFT,RIGHT

以上就是pack的常用参数,推荐自己试一试。下面介绍几种常用的参数的使用效果。

2.几个参数的使用效果示例

先写一个没有参数的栗子。

from tkinter import *

root = Tk()

lab1 = Label(root,text="第一个标签",bg="red")
lab2 = Label(root,text="第二二个标签",bg="yellow")
lab3 = Label(root,text="第三三三个标签",bg="green")

lab1.pack()
lab2.pack()
lab3.pack()

root.mainloop()

结果如下:


原始.png

(1)side的使用

还是上面的栗子,稍微改一下

lab1.pack(side=TOP)
lab2.pack(side=LEFT)
lab3.pack(side=BOTTOM)
side.png
可以看到,side会改变排列位置。其他的情况可以自己试试。

(2)fill的使用

将原始的栗子改一下:

lab1.pack(fill=X)
lab2.pack(fill=Y)
lab3.pack(fill=NONE)
fill.png

实际上,第二个标签是垂直方向填充的,只是这里看不出来,还是建议自己尝试。

(3)anchor的使用

lab1.pack(anchor=N)
lab2.pack(anchor=E)
lab3.pack(anchor=SW)
anchor.png

(4)综合使用

多种属性综合起来使用,可以得到自己想要的结果,建议自己多去尝试。
这里举一个小栗子。

lab1.pack(side=LEFT,fill=Y)
lab2.pack(fill=X)
lab3.pack(fill=BOTH,expand=True)
image.png
当窗口大小改变时
image.png
注意看看什么改变,什么没改变。起到作用的参数分别是哪些?

三、Grid布局

Grid布局是Tkinter后来引入的布局方式,相对来说,使用更方便简单。而且相比Pack布局,Grid布局在细节的调整上要更加强大。
Grid布局将容器空间分成一个个类似excel表格的单元格,按照行(row)列(column)的方式排列组件,组件位置由其行和列的值来决定:
行号相同而列号不同的几个组件会被依次上下排列
列号相同而行号不同的几个组件会被依次左右排列
使用Grid布局的过程就是为各个组件指定行号和列号的过程,不需要为每个网格指定大小,Grid布局会自动设置合适的大小。

1.Grid的参数

还是推荐使用python自带的help()来查看我们不太熟悉的函数。

>>> help(tkinter.Label.grid)
Help on function grid_configure in module tkinter:

grid_configure(self, cnf={}, **kw)
    Position a widget in the parent widget in a grid. Use as options:
    column=number - use cell identified with given column (starting with 0)
    columnspan=number - this widget will span several columns
    in=master - use master to contain this widget
    in_=master - see 'in' option description
    ipadx=amount - add internal padding in x direction
    ipady=amount - add internal padding in y direction
    padx=amount - add padding in x direction
    pady=amount - add padding in y direction
    row=number - use cell identified with given row (starting with 0)
    rowspan=number - this widget will span several rows
    sticky=NSEW - if cell is larger on which sides will this
                  widget stick to the cell boundary

上面的参数与Pack的很多都相同,其余的参数都很好理解,简单列在下面供参考:

方法名称 作用 可用的参数
row 指定行号 从0开始为第1行
column 指定列号 从0开始为第1列
rowspan 指定跨越的行的数量 默认为1,跨越多行则指定数值
columnspan 指定跨越的列的数量 默认为1,跨越多列则指定数值
sticky 指定组件粘在单元格哪个方位的边界上 与anchor一样

2.几个Grid的参数的演示

接Pack的第一个栗子,将布局方式稍微做一下改变:

lab1.grid(row=0,column=0)
lab2.grid(row=0,column=1)
lab3.grid(row=1,column=1)
grid.png

注意到,类似excel,列宽默认根据最宽的组件宽度来设置,行高也是一样的,这里没有演示,可以自己试试。

四、Place布局

Place布局就是其他GUI布局中的“绝对布局”,要求指定每个组件的绝对位置或者相对于其他组件的相对位置。

Place的参数

还是先看help:

>>> help(tkinter.Label.place)
Help on function place_configure in module tkinter:

place_configure(self, cnf={}, **kw)
    Place a widget in the parent widget. Use as options:
    in=master - master relative to which the widget is placed
    in_=master - see 'in' option description
    x=amount - locate anchor of this widget at position x of master
    y=amount - locate anchor of this widget at position y of master
    relx=amount - locate anchor of this widget between 0.0 and 1.0
                  relative to width of master (1.0 is right edge)
    rely=amount - locate anchor of this widget between 0.0 and 1.0
                  relative to height of master (1.0 is bottom edge)
    anchor=NSEW (or subset) - position anchor according to given direction
    width=amount - width of this widget in pixel
    height=amount - height of this widget in pixel
    relwidth=amount - width of this widget between 0.0 and 1.0
                      relative to width of master (1.0 is the same width
                      as the master)
    relheight=amount - height of this widget between 0.0 and 1.0                                                                  
                       relative to height of master (1.0 is the same
                       height as the master)
    bordermode="inside" or "outside" - whether to take border width of
                                       master widget into account

不做过多的介绍了,参数基本上能看懂,无非就是指定绝对的位置和相对的位置,而一般来说Place用得较少。

总结:整体来看,个人觉得使用的优先级是:Grid>Pack>Place

所以还是推荐Grid布局管理,方便快捷,整体设计起来比较方便。

上一篇 下一篇

猜你喜欢

热点阅读