一橄唬、簡(jiǎn)介
所謂布局管理法竞,就是對(duì)添加到窗口中的組件的大小和位置進(jìn)行設(shè)置耙厚。此外,當(dāng)用戶調(diào)整了窗口大小后岔霸,布局管理器還會(huì)自動(dòng)調(diào)整窗口中各個(gè)組件的大小和位置薛躬。
tkinter有三種布局管理器:
(1)Pack
(2)Gird
(3)Place
二、pack布局
使用pack布局時(shí)呆细,當(dāng)向窗口中添加組件時(shí)型宝,這些組件會(huì)依次向后排列,排列方向可以是水平的,也可以是垂直的趴酣。
1.pack的參數(shù)
通常我推薦使用python自帶的help()
來(lái)查看我們不太熟悉的函數(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(實(shí)際上是東南西北方位的簡(jiǎn)稱岖寞,上北下南左西右東 |
expand | 指定當(dāng)父級(jí)容器增大時(shí)是否拉伸組件 | 布爾值抡四,True,F(xiàn)alse |
fill | 設(shè)置組件是否延水平或垂直方向填充 | X,Y,BOTH,NONE |
ipadx | 指定組件在x方向(水平方向)的內(nèi)部留白 | 數(shù)值仗谆,表示像素 |
ipady | 指定組件在y方向(垂直方向)的內(nèi)部留白 | 數(shù)值指巡,表示像素 |
padx | 指定組件在x方向(水平方向)與其他組件的間距 | 數(shù)值,表示像素 |
pady | 指定組件在y方向(垂直方向)與其他組件的間距 | 數(shù)值隶垮,表示像素 |
side | 設(shè)置組件的添加位置 | TOP,BOTTOM,LEFT,RIGHT |
以上就是pack的常用參數(shù)藻雪,推薦自己試一試。下面介紹幾種常用的參數(shù)的使用效果岁疼。
2.幾個(gè)參數(shù)的使用效果示例
先寫一個(gè)沒(méi)有參數(shù)的栗子阔涉。
from tkinter import *
root = Tk()
lab1 = Label(root,text="第一個(gè)標(biāo)簽",bg="red")
lab2 = Label(root,text="第二二個(gè)標(biāo)簽",bg="yellow")
lab3 = Label(root,text="第三三三個(gè)標(biāo)簽",bg="green")
lab1.pack()
lab2.pack()
lab3.pack()
root.mainloop()
結(jié)果如下:
(1)side的使用
還是上面的栗子,稍微改一下
lab1.pack(side=TOP)
lab2.pack(side=LEFT)
lab3.pack(side=BOTTOM)
可以看到捷绒,side會(huì)改變排列位置瑰排。其他的情況可以自己試試。
(2)fill的使用
將原始的栗子改一下:
lab1.pack(fill=X)
lab2.pack(fill=Y)
lab3.pack(fill=NONE)
實(shí)際上暖侨,第二個(gè)標(biāo)簽是垂直方向填充的椭住,只是這里看不出來(lái),還是建議自己嘗試字逗。
(3)anchor的使用
lab1.pack(anchor=N)
lab2.pack(anchor=E)
lab3.pack(anchor=SW)
(4)綜合使用
多種屬性綜合起來(lái)使用京郑,可以得到自己想要的結(jié)果,建議自己多去嘗試葫掉。
這里舉一個(gè)小栗子些举。
lab1.pack(side=LEFT,fill=Y)
lab2.pack(fill=X)
lab3.pack(fill=BOTH,expand=True)
當(dāng)窗口大小改變時(shí)
注意看看什么改變,什么沒(méi)改變俭厚。起到作用的參數(shù)分別是哪些户魏?
三、Grid布局
Grid布局是Tkinter后來(lái)引入的布局方式挪挤,相對(duì)來(lái)說(shuō)叼丑,使用更方便簡(jiǎn)單。而且相比Pack布局扛门,Grid布局在細(xì)節(jié)的調(diào)整上要更加強(qiáng)大鸠信。
Grid布局將容器空間分成一個(gè)個(gè)類似excel表格的單元格,按照行(row)和列(column)的方式排列組件论寨,組件位置由其行和列的值來(lái)決定:
行號(hào)相同而列號(hào)不同的幾個(gè)組件會(huì)被依次上下排列
列號(hào)相同而行號(hào)不同的幾個(gè)組件會(huì)被依次左右排列
使用Grid布局的過(guò)程就是為各個(gè)組件指定行號(hào)和列號(hào)的過(guò)程星立,不需要為每個(gè)網(wǎng)格指定大小爽茴,Grid布局會(huì)自動(dòng)設(shè)置合適的大小。
1.Grid的參數(shù)
還是推薦使用python自帶的help()
來(lái)查看我們不太熟悉的函數(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ù)都很好理解,簡(jiǎn)單列在下面供參考:
方法名稱 | 作用 | 可用的參數(shù) |
---|---|---|
row | 指定行號(hào) | 從0開(kāi)始為第1行 |
column | 指定列號(hào) | 從0開(kāi)始為第1列 |
rowspan | 指定跨越的行的數(shù)量 | 默認(rèn)為1辕坝,跨越多行則指定數(shù)值 |
columnspan | 指定跨越的列的數(shù)量 | 默認(rèn)為1,跨越多列則指定數(shù)值 |
sticky | 指定組件粘在單元格哪個(gè)方位的邊界上 | 與anchor一樣 |
2.幾個(gè)Grid的參數(shù)的演示
接Pack的第一個(gè)栗子荐健,將布局方式稍微做一下改變:
lab1.grid(row=0,column=0)
lab2.grid(row=0,column=1)
lab3.grid(row=1,column=1)
注意到酱畅,類似excel,列寬默認(rèn)根據(jù)最寬的組件寬度來(lái)設(shè)置江场,行高也是一樣的纺酸,這里沒(méi)有演示,可以自己試試址否。
四餐蔬、Place布局
Place布局就是其他GUI布局中的“絕對(duì)布局”,要求指定每個(gè)組件的絕對(duì)位置或者相對(duì)于其他組件的相對(duì)位置佑附。
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
不做過(guò)多的介紹了樊诺,參數(shù)基本上能看懂,無(wú)非就是指定絕對(duì)的位置和相對(duì)的位置音同,而一般來(lái)說(shuō)Place用得較少词爬。
總結(jié):整體來(lái)看,個(gè)人覺(jué)得使用的優(yōu)先級(jí)是:Grid>Pack>Place
所以還是推薦Grid布局管理权均,方便快捷顿膨,整體設(shè)計(jì)起來(lái)比較方便。