這一章開始對tkinter的各個組件進(jìn)行學(xué)習(xí),之前第一篇中列出了tkinter的組件。
一、Label組件的簡介
Lable就是平時說的標(biāo)簽,實(shí)際上它既可以是文本標(biāo)簽,還可以是圖標(biāo)。
Label的參數(shù)
還是使用help查看一下(內(nèi)容非常的多,有好幾頁,這里只選取部分展示):
Label(master=None, cnf={}, **kw)
|
| Label widget which can display text and bitmaps.
| # 標(biāo)簽小部件,可以顯示文本和位圖。
| __init__(self, master=None, cnf={}, **kw)
| Construct a label widget with the parent MASTER.
|
| STANDARD OPTIONS
| # 標(biāo)準(zhǔn)選項
| activebackground, activeforeground, anchor,
| background, bitmap, borderwidth, cursor,
| disabledforeground, font, foreground,
| highlightbackground, highlightcolor,
| highlightthickness, image, justify,
| padx, pady, relief, takefocus, text,
| textvariable, underline, wraplength
|
| WIDGET-SPECIFIC OPTIONS
| # 特定選項
| height, state, width
可以看到,Label有很多的參數(shù),但是大部分都是通用的參數(shù)。
之前的栗子里面已經(jīng)看過了Label的構(gòu)建,這里不再做這個演示。
二、組件的通用屬性介紹
在前面的篇幅已經(jīng)列出來過組件的通用屬性,這里結(jié)合Label進(jìn)行進(jìn)一步的介紹。
1.顏色:fg和bg
fg或foreground:可以設(shè)置前景色彩,在此相當(dāng)于是標(biāo)簽的顏色。
bg或background:可以設(shè)置背景色彩。
from tkinter import *
root = Tk()
lab1 = Label(root,text="第一個標(biāo)簽",bg="red")
lab2 = Label(root,text="第二二個標(biāo)簽",bg="yellow")
lab1.pack()
lab2.pack()
root.mainloop()

2.尺寸:height和width
height可以設(shè)置Widget控件的高度,單位是字符高度。
width可以設(shè)置Widget控件的寬度,單位是字符寬度。
lab1 = Label(root,text="第一個標(biāo)簽",bg="red",height=3,width=20)
lab2 = Label(root,text="第二二個標(biāo)",bg="yellow",height=5,width=10)

3.Anchor
前面介紹過,不再進(jìn)行講解。
相對的位置如下圖:

4.wraplength
可以設(shè)置每行顯示的最大字符數(shù)。
lab1 = Label(root,text="第一個標(biāo)簽",bg="red",height=3,width=20,wraplength=40)
lab2 = Label(root,text="第二二個標(biāo)簽",bg="yellow",height=5,width=10,wraplength=10)

5.font
指定組件上顯示的文本字體。
這里有很多選項:
| 屬性 | 參數(shù) |
|---|---|
| family | 字形。如Helvetica、Times等,可以進(jìn)入Word內(nèi)參考所有系統(tǒng)字形 |
| size | 字號。單位是像素 |
| weight | 例如bold、normal |
| slant | 例如italic、roman,如果不是italic則是roman |
| underline | 下劃線。例如True、False |
| overstrike | 刪除線。例如True、False |
通常將上面的參數(shù)用空格分開,組成一個字符串傳給font。
lab1 = Label(root,text="第一個標(biāo)簽",bg="red",font="Times 20 bold")

6.justify
指定組件內(nèi)部內(nèi)容的對齊方式,支持LEFT,RIGHT,CENTER這三個值。
左對齊
lab1 = Label(root,text="1234567890",bg="red",wraplength=30,justify=LEFT)

右對齊
lab1 = Label(root,text="1234567890",bg="red",wraplength=30,justify=RIGHT)

居中對齊
lab1 = Label(root,text="1234567890",bg="red",wraplength=30,justify=CENTER)

7.Bitmaps
指定在組件上顯示該選項指定的位圖。
該選項可以是Tk_GetBitmap接收的任何形式的位圖。
有3點(diǎn)需要注意:
- 位圖的顯示方式受anchor和justify的影響;
- 如果同時指定了Bitmap和text,那么bitmap覆蓋文本;
-
如果同時指定了Bitmap和image,那么image覆蓋bitmap。
下面是tkinter提供的在標(biāo)簽位置放置內(nèi)建位圖:
tkinter內(nèi)建位圖.png
下面是一個小栗子:
lab1 = Label(root,text="1234567890",bitmap="warning")

注意到,text的文本被圖像覆蓋掉了。
8.compound
當(dāng)需要圖像與文字共存時,可以使用此參數(shù)定義文字與圖像的位置關(guān)系。
compound參數(shù)可以是下列值。
- left:圖像在左。
- right:圖像在右。
- top:圖像在上。
- bottom:圖像在下。
- center:文字覆蓋在圖像上方。
示例:
lab1 = Label(root,text="1234567890",bitmap="warning",compound=LEFT)

9.relief
指定組件的3D效果,該選項包括的值有:
| relief可選的值 | 效果 |
|---|---|
| FLAT | 平坦的 |
| RAISED | 凸起的 |
| SUNKEN | 內(nèi)凹的 |
| RIDGE | 鑲嵌的 |
| SOLID | 實(shí)心的 |
| GROOVE | 開槽的 |
可以自己試試,大致效果如下:

10.image
指定組件中顯示的圖像。
通??梢韵仍O(shè)置PhotoImage圖像對象,再將此圖像對象應(yīng)用作為參數(shù)傳給image。
pictest = PhotoImage(file = "C:\\Users\\Desktop\\1.png")
lab1 = Label(root,text="1234567890",image=pictest)

注意到,image將text覆蓋了。
如果不想被覆蓋,可以使用compound。
11.config
用于更改組件的屬性。
在建立時沒有直接設(shè)置的屬性,可以再后面更改使用config( )方法屬性,實(shí)現(xiàn)了組件屬性的動態(tài)修改。
12.cursor
指定鼠標(biāo)光標(biāo)在組件上的樣式。
其值可以是Tk_GetCursor()接收的任何格式。
13.keys()
對任意組件使用keys()可以得到該組件可用的參數(shù)的列表(List)。
>>> from tkinter import *
>>> root = Tk()
>>> lab1=Label(root,text="123")
>>> print(lab1.keys())
['activebackground', 'activeforeground', 'anchor', 'background', 'bd', 'bg', 'bitmap', 'borderwidth',
'compound', 'cursor', 'disabledforeground', 'fg', 'font', 'foreground', 'height', 'highlightbackground',
'highlightcolor', 'highlightthickness', 'image', 'justify', 'padx', 'pady', 'relief', 'state', 'takefocus', 'text',
'textvariable', 'underline', 'width', 'wraplength']
14.Separator
分隔線,tkinter.ttk中有Separator模塊。
lab1 = Label(root,text="1234567890")
lab2 = Label(root,text="第二二個標(biāo)簽")
sep1 = Separator(root,orient=HORIZONTAL)
lab1.pack()
sep1.pack(fill=X)
lab2.pack()

