tkinter-label

本文介紹tkinter中label控件的使用。結合實際的示例來理解label控件的使用,看完本文基本上就能理解label各個屬性的含義並能靈活使用,做到心中有譜。

標籤(label)

標籤(label)控件中可以顯示文字和圖像。label顯示的文本內容是可以動態更新。

標籤屬性

background\bg,

表示顯示區的背景色

# bg 設置顯示區的背景顏色
# 如果沒有指定顯示區的寬高,label會自適應text文字的寬高
label1 = Label(main_win, bg='white', text='hello', anchor='center')
label1.pack()
# 如果label顯示區是自適應文字的寬高,那麼設置anchor其實沒有實際作用
label2 = Label(main_win, bg='white', text='hello', anchor='se')
label2.pack()

在這裏插入圖片描述

text

指定顯示的文本,文本中的’\n’將會換行。

width

指定顯示區的寬度,如果沒有指定,將自適應文本實際的寬度。如果顯示區的內容是文本,那麼以字符爲單位;

如果顯示區的內容是圖片,那麼以像素爲單位。

height

指定顯示區的高度

anchor

錨點,用來指定文本或圖像在label顯示區的顯示位置。默認值是"center",可設置的值爲’n’, ‘ne’, ‘e’, ‘se’, ‘s’, ‘sw’, ‘w’, ‘nw’; ‘e’、‘w’、‘s’、'n’分別表示東西南北。

# width height 設置顯示區的寬高
# 這裏我們將顯示區的寬和高設置爲40和10,anchor爲'se'
# 最終的顯示效果是 hello在顯示區的東南方,也就是右下角;
# 另外寬和高的單位不是像素,而是字符.例如,顯示區的寬度爲40個字符的寬度
label1 = Label(main_win, width=40, height=10, bg='white', text='hello', anchor='se')
label1.pack()

在這裏插入圖片描述

foreground\fg

指定文本顯示顏色

# fg 將前景色設置成紅色, 即設置文本的顏色爲紅色
# 同時將anchor設置成'ne', 將文本顯示在東北方,也就是右上角
label1 = Label(main_win, width=40, height=10, fg='red', bg='white', text='hello', anchor='ne')
label1.pack()

在這裏插入圖片描述

font

指定字體

# font 設置字體.
# 從顯示效果可以看出,顯示區的寬和高雖然設置成了固定的數值,但是隨着字體的變大,顯示區域也跟着變大了
# 爲什麼?因爲寬和高的單位是用字符來度量了,如果字符變大了,那麼label的顯示區域也就跟着變大了
label1 = Label(main_win, font=("Courier", 20, "bold"), width=40, height=10, fg='red', bg='white', text='hello', anchor='ne')
label1.pack()

在這裏插入圖片描述

cursor

指定鼠標在標籤顯示區域時的顯示樣式,樣式的值是’arrow’, ‘watch’, ‘cross’

label1 = Label(main_win, cursor="cross", width=40, height=10, fg='red', bg='white', text='hello', anchor='ne')
label1.pack()

image

給label指定顯示的圖片

image屬性只支持四種格式的圖片PGM, PPM, GIF, PNG

如果要顯示其他格式的圖片,需要藉助另外的庫來處理,例如要顯示jpg圖片,就需要PIL庫。

# 將主窗口的尺寸設置大一些,使其容納兩張圖片
main_win.geometry(f'{800}x{800}')
img_list = []
# 使用PhotoImage()讀取圖片數據
gif = PhotoImage(file='test.gif')
png = PhotoImage(file='qq.png')
# 將圖片數據放入列表
img_list.append(gif)
img_list.append(png)
# 顯示列表中的圖片
for img in img_list:
    Label(main_win, image=img).pack()

在這裏插入圖片描述

jpg = PhotoImage(file='github.jpg')
Label(main_win, image=jpg).pack()

# 執行後會報錯:_tkinter.TclError: couldn't recognize data in image file "github.jpg"
# 也就驗證了我前面提到的image只支持直接顯示4種格式的圖片
# 那麼問題來了,我們要怎麼顯示jpg格式的圖片呢?
main_win.geometry(f'{800}x{800}')
# 顯示png圖片需要使用PIL庫將圖片數據轉換爲Label可識別的image
img = ImageTk.PhotoImage(Image.open(r'github.jpg'))
label1 = Label(main_win, image=img, bg='blue')
label1.pack()

在這裏插入圖片描述

bitmap

從屬性名上可以知道,該屬性是用來顯示位圖的。是否能顯示常見的圖片呢?我們通過代碼試試。

# bitmap
# 顯示tkinter內置的位圖
bitmap_list = ['error', 'hourglass', 'questhead', 'info',
                'question', 'warning', 'gray12', 'gray50', 
                'gray75']
for bitmap in bitmap_list:
    Label(main_win, bitmap=bitmap).pack()

# 通過本例可以知道,bitmap屬性的值是字符串

在這裏插入圖片描述

bitmap屬性的值是字符串,字符串值只有兩種,一種就是前面使用的內置位圖的字符串,另一種是使用 @+xbm後綴的文件名。

重點:僅支持xbm格式且xbm的路徑要使用@開頭

xbm = 'qq.xbm'
# 將qq.png轉換爲單通道的qq.xbm圖片
Image.open('qq.png').convert("1").save(xbm)
Label(main_win, bitmap='@'+xbm).pack()

在這裏插入圖片描述

borderwidth\bd

指定標籤邊框寬度

padx

指定水平方向上內容和邊框的間距

pady

指定垂直方向上內容和邊框的間距

下圖即是label內部不同屬性的構成:
在這裏插入圖片描述

main_win.geometry(f'{800}x{800}')
label1 = Label(main_win, text="漁道", fg='blue', background="green")
label1.pack()

label2 = Label(main_win, text="漁道", fg='blue', background="green",
                width = 20, height=10)
label2.pack()

# borderwidth
label3 = Label(main_win, text="漁道", fg='blue', background="green",
                width = 20, height=10, borderwidth=10)
label3.pack()

# padx pady
label4 = Label(main_win, text="漁道", fg='blue', background="green",
                width = 20, height=10, borderwidth=10,
                padx = 10, pady = 10)
label4.pack()

上面例子的顯示結果可以看出,如果設置bd、padx、pady,label的顯示區會相應變大.
在這裏插入圖片描述

relief

指定邊框樣式,默認樣式爲’flat’,樣式的種類有:‘flat’, ‘groove’, ‘raised’, ‘ridge’, ‘solid’, ‘sunken’

如果bd的寬度變大,relief顯示的邊框也會跟着變大。

# relief
main_win.geometry(f'{800}x{800}')
relief_list = ['flat', 'groove', 'raised', 'ridge', 'solid', 'sunken']

for relief in relief_list:
    Label(main_win, text="漁道", fg='red', background="green",
                width = 10, height=5, borderwidth=10, 
                padx=10, pady=10, relief=relief).pack()

在這裏插入圖片描述

justify

定義對齊,"left"左對齊,"center"居中,"right"右對齊

# justify
poem = '''
            將進酒
             李白
君不見黃河之水天上來,奔流到海不復回。
君不見高堂明鏡悲白髮,朝如青絲暮成雪。
人生得意須盡歡,莫使金樽空對月。
天生我材必有用,千金散盡還復來。
烹羊宰牛且爲樂,會須一飲三百杯。
岑夫子,丹丘生,將進酒,杯莫停。
與君歌一曲,請君爲我傾耳聽。
鐘鼓饌玉不足貴,但願長醉不願醒。
古來聖賢皆寂寞,惟有飲者留其名。
陳王昔時宴平樂,斗酒十千恣歡謔。
主人何爲言少錢,徑須沽取對君酌。
五花馬、千金裘,呼兒將出換美酒,與爾同銷萬古愁。
'''
main_win.geometry(f'{800}x{800}')
Label(main_win, text=poem, fg='white', justify="center", background="blue",
            borderwidth=10, 
            padx=10, pady=10, relief='ridge').pack()

Label(main_win, text=poem, fg='white', justify="left", background="blue",

            borderwidth=10, 
            padx=10, pady=10, relief='ridge').pack()

在這裏插入圖片描述

state

指定label的狀態,normal(默認)/active/disable

activebackground

指定label爲active狀態時的背景顏色

activeforeground

指定label爲active狀態時的前景顏色

label1 = Label(main_win, text='漁道', bg='white', state='active', 
            activeforeground='red', activebackground='green')
label1.pack()

label2= Label(main_win, text='漁道', bg='white', state='normal', 
            activeforeground='red', activebackground='green')
label2.pack()

label1標籤的state爲active,背景顯示爲綠色,文字顯示位紅色

label2標籤的state爲normal,背景顯示爲白色,文字顯示爲默認的黑色

可以看出只有在控件的state爲active時,activeforeground和activebackground纔會生效。

在這裏插入圖片描述

disabledforeground

當標籤的狀態爲DISABLE時,文字的顯示顏色

label1 = Label(main_win, text='漁道', bg='white', state='disable', 
            disabledforeground='red', activebackground='green')
label1.pack()

label2= Label(main_win, text='漁道', bg='white', state='normal', 
            activeforeground='red', activebackground='green')
label2.pack()

在這裏插入圖片描述

textvariable

顯示 StringVar變量的內容,作用是可以動態的更新label顯示的內容。

text_var = StringVar() 
text_var.set("你好,漁道")
label1 = Label(main_win, width=40, height=10, bg='white', textvariable=text_var, anchor='se')
text_var.set("你好,python")
label1.pack()

從下圖的執行結果,我們可以看出,'你好,python’將’你好,漁道’覆蓋了,就是因爲動態更新了。

在這裏插入圖片描述

underline

指定下劃線,從第0個字符開始直到underline指定的n。默認underline爲-1表示不顯示下劃線。

label1 = Label(main_win, width=40, height=10, bg='white', text='hello', underline=2)
label1.pack()

從下圖可以看出,第一個字母l下有下劃線顯示

在這裏插入圖片描述

wraplength

指定每一個行最大的像素數。注意了!是以像素爲單位,而不是字符。

txt = '12345678'
label1 = Label(main_win, bg='white', text=txt, wraplength=10)
label1.pack()

在這裏插入圖片描述

好了,標籤的屬性就介紹到這裏。

創作不易,如果你有所收穫,請不吝點贊,如有不足之處,歡迎下方評論,謝謝!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章