Tkinter自學02:tkinter 的3種布局管理器

一、簡介

所謂布局管理,就是對添加到窗口中的組件的大小和位置進行設置。此外,當用戶調(diào)整了窗口大小后,布局管理器還會自動調(diào)整窗口中各個組件的大小和位置。
tkinter有三種布局管理器:
(1)Pack
(2)Gird
(3)Place

二、pack布局

使用pack布局時,當向窗口中添加組件時,這些組件會依次向后排列,排列方向可以是水平的,也可以是垂直的。

1.pack的參數(shù)

通常我推薦使用python自帶的help()來查看我們不太熟悉的函數(shù)。

>>> 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通常支持以下的方法:

方法名稱 作用 可用的參數(shù)
anchor 根據(jù)給定方向放置位置小部件 N,W,S,E,NW,NE,SW,SE,CENTER(實際上是東南西北方位的簡稱,上北下南左西右東
expand 指定當父級容器增大時是否拉伸組件 布爾值,True,F(xiàn)alse
fill 設置組件是否延水平或垂直方向填充 X,Y,BOTH,NONE
ipadx 指定組件在x方向(水平方向)的內(nèi)部留白 數(shù)值,表示像素
ipady 指定組件在y方向(垂直方向)的內(nèi)部留白 數(shù)值,表示像素
padx 指定組件在x方向(水平方向)與其他組件的間距 數(shù)值,表示像素
pady 指定組件在y方向(垂直方向)與其他組件的間距 數(shù)值,表示像素
side 設置組件的添加位置 TOP,BOTTOM,LEFT,RIGHT

以上就是pack的常用參數(shù),推薦自己試一試。下面介紹幾種常用的參數(shù)的使用效果。

2.幾個參數(shù)的使用效果示例

先寫一個沒有參數(shù)的栗子。

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

結(jié)果如下:


原始.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)綜合使用

多種屬性綜合起來使用,可以得到自己想要的結(jié)果,建議自己多去嘗試。
這里舉一個小栗子。

lab1.pack(side=LEFT,fill=Y)
lab2.pack(fill=X)
lab3.pack(fill=BOTH,expand=True)

image.png

當窗口大小改變時
image.png

注意看看什么改變,什么沒改變。起到作用的參數(shù)分別是哪些?

三、Grid布局

Grid布局是Tkinter后來引入的布局方式,相對來說,使用更方便簡單。而且相比Pack布局,Grid布局在細節(jié)的調(diào)整上要更加強大。
Grid布局將容器空間分成一個個類似excel表格的單元格,按照行(row)列(column)的方式排列組件,組件位置由其行和列的值來決定:
行號相同而列號不同的幾個組件會被依次上下排列
列號相同而行號不同的幾個組件會被依次左右排列
使用Grid布局的過程就是為各個組件指定行號和列號的過程,不需要為每個網(wǎng)格指定大小,Grid布局會自動設置合適的大小。

1.Grid的參數(shù)

還是推薦使用python自帶的help()來查看我們不太熟悉的函數(shù)。

>>> 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

上面的參數(shù)與Pack的很多都相同,其余的參數(shù)都很好理解,簡單列在下面供參考:

方法名稱 作用 可用的參數(shù)
row 指定行號 從0開始為第1行
column 指定列號 從0開始為第1列
rowspan 指定跨越的行的數(shù)量 默認為1,跨越多行則指定數(shù)值
columnspan 指定跨越的列的數(shù)量 默認為1,跨越多列則指定數(shù)值
sticky 指定組件粘在單元格哪個方位的邊界上 與anchor一樣

2.幾個Grid的參數(shù)的演示

接Pack的第一個栗子,將布局方式稍微做一下改變:

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

注意到,類似excel,列寬默認根據(jù)最寬的組件寬度來設置,行高也是一樣的,這里沒有演示,可以自己試試。

四、Place布局

Place布局就是其他GUI布局中的“絕對布局”,要求指定每個組件的絕對位置或者相對于其他組件的相對位置。

Place的參數(shù)

還是先看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

不做過多的介紹了,參數(shù)基本上能看懂,無非就是指定絕對的位置和相對的位置,而一般來說Place用得較少。

總結(jié):整體來看,個人覺得使用的優(yōu)先級是:Grid>Pack>Place

所以還是推薦Grid布局管理,方便快捷,整體設計起來比較方便。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容