tkinter組件詳解之Label


tkinter組件詳解之Label

Label組件用於在屏幕上顯示文本或圖像。最紅呈現出的結果是由背景和前景疊加構成的。

函數定義:Label(master=None, cnf={}, **kw)

背景

背景由三部分構成:內容、填充區、邊框

內容區的參數:width、height,用於指定區域的大小,單位依據前景的具體內容而變化(前景內容是文字--->單位:字符,前景內容是圖片--->單位:像素)

填充區的參數:padx、pady,用於指定內容與邊框之間的距離,單位:像素

邊框區的參數:relief,用於指定邊框區的樣式( 可選值為:flat(默認),sunken,raised,groove,ridge )

borderwidth:用於指定邊框的寬度,單位:像素

下面用一張圖來說明背景區:

設置Label的背景屬性

import tkinter as tk
win = tk.Tk()
win.title("Label的基本使用")
label = tk.Label(win, text="Label的基本使用", background="red",
                 padx=0, pady=0, borderwidth=10, relief="ridge")
label.pack()
win.mainloop()
import tkinter as tk
win = tk.Tk()
win.title("Label的基本使用")
label = tk.Label(win, text="Label的基本使用", background="red",
                 padx=50, pady=50, borderwidth=10, relief="ridge")
label.pack()
win.mainloop()
代碼一 代碼二

|

前景

前景分為文本和圖片兩部分

文本

文本參數:font=(font_name, size),指定字體和字體大小

justify = "center(默認)left/right/" ,指定文本的對齊方式

foreground = "指定的顏色" ,指定文字的顏色

text = "文本內容",指定文本的內容(靜態的)

anchor = " n/s/w/e/ne/nw/sw/se/center(默認) ",指定文本在背景內容區的位置(n:北、w:西、e:東、s:南)

設置文本的屬性

import tkinter as tk

win = tk.Tk()
win.title("Label的基本使用")
text = """Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!"""
label = tk.Label(win, text=text, background="red",
                 font=("黑體", 16), justify="left", foreground="blue")
label.pack()
win.mainloop()

圖片

圖片參數: image = normal_image(僅支持GIF, PPM/PGM格式的圖片),通常需要從Pillow庫中引入Image和ImageTk,使用如下語句來轉換為支持的圖片格式。

from PIL import Image
from PIL import ImageTk
import tkinter
# imagePath為圖片保存的路徑
img = Image.open(imagePath)
img = ImageTk.PhotoImage(img)

compound = "bottom/top/left/right/center/None(默認)",bottom/top/left/right表示圖片顯示在文本的下/上/左/右,center表示文本顯示在圖片的上面

設置圖片屬性

import tkinter as tk
import os
from PIL import Image
from PIL import ImageTk

win = tk.Tk()
win.title("Label的基本使用")
print(os.getcwd())
image_path = os.path.join(os.getcwd(), r"image\03.jpg")
img = Image.open(image_path)
img = ImageTk.PhotoImage(img)
label = tk.Label(win, text="Hello Python", font=("黑體", 26), foreground="blue",
                 image=img, compound="center")
label.pack()
win.mainloop()

參數列表

activebackground 設置當 Label 處於活動狀態(通過 state 選項設置狀態)的背景色
activeforeground 設置當 Label 處於活動狀態(通過 state 選項設置狀態)的前景色
anchor 控制文本(或圖像)在 Label 中顯示的位置
background 設置背景顏色
bitmap 指定顯示到 Label 上的位圖
borderwidth 指定 Label 的邊框寬度
cursor 指定當鼠標在 Label 上飄過的時候的鼠標樣式
disabledforeground 指定當 Label 不可用的時候前景色的顏色
font 指定 Label 中文本的字體
foreground 設置 Label 的文本和位圖的顏色
highlightbackground 指定當 Label 沒有獲得焦點的時候高亮邊框的顏色
highlightcolor 指定當 Label 獲得焦點的時候高亮邊框的顏色
highlightthickness 指定高亮邊框的寬度
image 指定 Label 顯示的圖片
justify 定義如何對齊多行文本
padx,pady 指定 Label 水平方向,垂直方向上的額外間距
relief 指定邊框樣式
takefocus 如果是 True,該 Label 接受輸入焦點
text 指定 Label 顯示的文本
textvariable Label 顯示 Tkinter 變量(通常是一個 StringVar 變量)的內容,如果變量被修改,Label 的文本會自動更新
underline 跟 text 選項一起使用,用於指定哪一個字符畫下划線
wraplength 決定 Label 的文本應該被分成多少行
height 設置 Label 的高度
state 指定 Label 的狀態
width 設置 Label 的寬度


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM