文章目錄
- 1、Label 簡介
- 2、Label的初步運用
- 3、Widget共同屬性Color
- 4、Widget的共同屬性 Dimensions
- 5、Widget的共同屬性 Anchor
- 6、Label 文字輸出換行位置 wraplength
- 7、Widget的共同屬性 Font
- 8、Label 的 justify參數
- 9、Widget 的共同屬性 Bitmaps
- 10、compound 參數
- 11、Widget 的共同屬性 relief
- 12、標籤文字與標籤區間的間距 padx/pady
- 13、圖像 PhotoImage
- 14、Widget 的共同方法 config()
- 15、Widget 的共同屬性 Cursors
- 16、Widget 的共同方法 keys()
- 17、分割線 Separator
1、Label 簡介
Label()
方法可以用於在窗口內建立文字
或圖像
標籤
它的語法格式如下:
Label(父對象, options, ...)
參數:
- 父對象:表示這個標籤將建立在哪一個父對象(可想成父窗口或者容器內)
- options參數,如下
options參數 | 含義 |
---|---|
activebackground | 1. 設置當 Label 處於活動狀態(通過 state 選項設置狀態)的背景色 2. 默認值由系統指定 |
activeforeground | 1. 設置當 Label 處於活動狀態(通過 state 選項設置狀態)的前景色 2. 默認值由系統指定 |
anchor | 1. 控制文本(或圖像)在 Label 中顯示的位置 2. “n”, “ne”, “e”, “se”, “s”, “sw”, “w”, “nw”, 或者 “center” 來定位(ewsn 代表東西南北,上北下南左西右東) 3. 默認值是 “center” |
background(bg) | 1. 設置背景顏色 2. 默認值由系統指定 |
bitmap | 1. 指定顯示到 Label 上的位圖 2. 如果指定了 image 選項,則該選項被忽略 |
borderwidth(bd) | 1. 指定 Label 的邊框寬度 2. 默認值由系統指定,通常是 1 或 2 像素 |
compound | 1. 控制 Label 中文本和圖像的混合模式 2. 默認情況下,如果有指定位圖或圖片,則不顯示文本 3. 如果該選項設置爲 “center”,文本顯示在圖像上(文本重疊圖像) 4. 如果該選項設置爲 “bottom”,“left”,“right” 或 “top”,那麼圖像顯示在文本的旁邊(如 “bottom”,則圖像在文本的下方) 5. 默認值是 NONE |
cursor | 1. 指定當鼠標在 Label 上飄過的時候的鼠標樣式 2. 默認值由系統指定 |
disabledforeground | 1. 指定當 Label 不可用的時候前景色的顏色 2. 默認值由系統指定 |
font | 1. 指定 Label 中文本的字體(注:如果同時設置字體和大小,應該用元組包起來,如(“楷體”, 20) 2. 一個 Label 只能設置一種字體 3. 默認值由系統指定 |
foreground(fg) | 1. 設置 Label 的文本和位圖的顏色 2. 默認值由系統指定 |
height | 1. 設置 Label 的高度 2. 如果 Label 顯示的是文本,那麼單位是文本單元 3. 如果 Label 顯示的是圖像,那麼單位是像素(或屏幕單元) 4. 如果設置爲 0 或者乾脆不設置,那麼會自動根據 Label 的內容計算出高度 |
highlightbackground | 1. 指定當 Label 沒有獲得焦點的時候高亮邊框的顏色 2. 默認值由系統指定,通常是標準背景顏色 |
highlightcolor | 1. 指定當 Label 獲得焦點的時候高亮邊框的顏色 2. 默認值由系統指定 |
highlightthickness | 1. 指定高亮邊框的寬度 2. 默認值是 0(不帶高亮邊框) |
image | 1. 指定 Label 顯示的圖片 2. 該值應該是 PhotoImage,BitmapImage,或者能兼容的對象 3. 該選項優先於 text 和 bitmap 選項 |
justify | 1. 定義如何對齊多行文本 2. 使用 “left”,“right” 或 "center" 3. 注意,文本的位置取決於 anchor 選項 4. 默認值是 “center” |
padx | 1. 指定 Label 水平方向上的額外間距(內容和邊框間) 2. 單位是像素 |
pady | 1. 指定 Label 垂直方向上的額外間距(內容和邊框間) 2. 單位是像素 |
relief | 1. 指定邊框樣式(浮雕) 2. 默認值是 “flat” 3. 另外你還可以設置 “groove”, “raised”, “ridge”, “solid” 或者 “sunken” |
state | 1. 指定 Label 的狀態 2. 這個標籤控制 Label 如何顯示 3. 默認值是 "normal 4. 另外你還可以設置 “active” 或 “disabled” |
takefocus | 1. 如果是 True,該 Label 接受輸入焦點 2. 默認值是 False |
text | 1. 指定 Label 顯示的文本 2. 文本可以包含換行符 3. 如果設置了 bitmap 或 image 選項,該選項則被忽略 |
textvariable | 1. Label 顯示 Tkinter 變量(通常是一個 StringVar 變量)的內容 2. 如果變量被修改,Label 的文本會自動更新 |
underline | 1. 跟 text 選項一起使用,用於指定哪一個字符畫下劃線(例如用於表示鍵盤快捷鍵) 2. 默認值是 -1 3. 例如設置爲 1,則說明在 Button 的第 2 個字符處畫下劃線 |
width | 1. 設置 Label 的寬度 2. 如果 Label 顯示的是文本,那麼單位是文本單元 3. 如果 Label 顯示的是圖像,那麼單位是像素(或屏幕單元) 4. 如果設置爲 0 或者乾脆不設置,那麼會自動根據 Label 的內容計算出寬度 |
wraplength | 1. 決定 Label 的文本應該被分成多少行 2. 該選項指定每行的長度,單位是屏幕單元 3. 默認值是 0 |
2、Label的初步運用
列子
:建立一個標籤,內容是"tkinter"
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="tkinter")
"""包裝與定位組件"""
label.pack()
"""傳回Label數據類型"""
print(type(label))
# 放在程序最後一行,使程序繼續執行
root.mainloop()
運行結果:
可以看到窗口是最小化的,但是沒關係,我們可以拖拽它
它的type類型爲:
<class 'tkinter.Label'>
關於這個pack()
我們可以用這種Label().pack()
的方式來重新設計程序
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="tkinter").pack()
print(type(label))
# 放在程序最後一行,使程序繼續執行
root.mainloop()
放心,也可以運行,但是!!!它的type類型可就變了
<class 'NoneType'>
不再是我們之前的tkinter.Label
類型了,這對於我們以後添加屬性是很不利
的,所以我們不推薦這種方法
3、Widget共同屬性Color
所謂Widget
,就是控件
或者組件
或者部件
,窗口建立完成後,下一步是在窗口內建立控件。比如Label
、Button
等等,這些全部都是Widget
共同屬性:所以控件都有的屬性
fg
或者foreground
:可以設置前景色彩,在此相當於是標籤的顏色bg
或者background
:可以設置背景色彩
例子
:設置文字前景色是紅色, 背景色是黃色。
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="tkinter", fg="red", bg="yellow")
label.pack()
root.mainloop()
運行結果:
4、Widget的共同屬性 Dimensions
height
可以設置Widget
控件的高度,單位是字符高度
width
可以設置Widget
控件的寬度,單位是字符寬度
例子
:設置標籤寬度爲15,高度爲3,背景色是黃色,前景色是紅色
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="tkinter", fg="red", bg="yellow",
height=3, width=15)
label.pack()
root.mainloop()
運行結果:
5、Widget的共同屬性 Anchor
Anchor
其實是指標籤文字在標籤區域輸出位置的設置,在默認情況下 Widget 控件是上下與左右都居中對齊。我們也可以使用anchor選項設定 Widget 控件的對齊
例子
:讓字符串從標籤區間左上角位置輸出
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="tkinter", fg="red", bg="yellow",
height=3, width=15,
anchor="nw")
label.pack()
root.mainloop()
運行結果:
例子
:讓字符串從標籤區間右下角位置輸出
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="tkinter", fg="red", bg="yellow",
height=3, width=15,
anchor="se")
label.pack()
root.mainloop()
運行結果:
注:anchor的參數設置也可以使用內建大寫常數,例如,nw
使用NW
、n
使用N
、ne
使用NE
、w
使用W
、center
使用CENTER
、e
使用E
、sw
使用SW
、s
使用S
、se
使用SE
。 當程序使用大寫常數時,可以省略
字符串的雙引號。
例子:
anchor=tkinter.SE
運行結果跟上面相同
6、Label 文字輸出換行位置 wraplength
wraplength 這個參數可以設置標籤中的文字在多少寬度後自動換行
例子
:讓標籤中的文字達到20像素寬度後自動換行
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="tkinter", fg="red", bg="yellow",
height=3, width=15,
anchor=tkinter.NW,
wraplength=20)
label.pack()
root.mainloop()
運行結果:
7、Widget的共同屬性 Font
font 參數用於設置文字字形
參數 | 含義 |
---|---|
字形family | 如Helvetica、Times等 |
字號size | 單位是像素 |
weight | 例如bold、normal |
slant | 例如italic、roman,如果不是italic則是roman |
underline | 例如True、False |
overstrike | 例如True、False |
例子
:使用Helvetica字形,大小是20,粗體顯示
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="tkinter", fg="red", bg="yellow",
height=3, width=15,
font="Helvetic 20 bold")
label.pack()
root.mainloop()
運行結果:
其實font 可以用元組的形式,這樣更直觀
font=("Helvetic", 20, "bold")
結果跟上面一樣的
8、Label 的 justify參數
在標籤的輸出中,如果是多行的輸出,在最後一行輸出時可以使用justify
參數設置所輸出的標籤內容是lef/center/right
( 靠左/居中/靠右
),默認是居中輸出。
例子
:使用默認方式執行多行輸出
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="qwertyuiopasdfghjklzxcvbnm", fg="red", bg="yellow",
wraplength=50)
label.pack()
root.mainloop()
運行結果:
可以發現最後一行是默認居中的
例子
:設置最後一行是靠左對齊輸出
label = tkinter.Label(root, text="qwertyuiopasdfghjklzxcvbnm", fg="red", bg="yellow",
wraplength=50,
justify="left")
運行結果:
例子
:設置最後一行是靠右對齊輸出
label = tkinter.Label(root, text="qwertyuiopasdfghjklzxcvbnm", fg="red", bg="yellow",
wraplength=50,
justify="right")
運行結果:
9、Widget 的共同屬性 Bitmaps
tkinter
也提供了在標籤
位置放置內建位圖
功能
名稱 | 圖形 |
---|---|
error | |
hourglass | |
info | |
questhead | |
question | |
warning | |
gray12 | |
gray25 | |
gray50 | |
gray75 |
例子
:在標籤位置顯示error的位圖
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, bitmap="error")
label.pack()
root.mainloop()
運行結果:
10、compound 參數
當圖像與文字共存時,可以使用此參數定義文字與圖像的位置關係
參數 | 含義 |
---|---|
left | 圖像在左 |
right | 圖像在右 |
top | 圖像在上 |
bottom | 圖像在下 |
center | 文字覆蓋在圖像上方 |
例子
:圖像與文字共存時,圖像在左邊
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, bitmap="error",
compound="left", text="我很喜歡寫博客")
label.pack()
root.mainloop()
運行結果:
例子
:圖像與文字共存時,圖像在上方
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, bitmap="error",
compound="top", text="我很喜歡寫博客")
label.pack()
root.mainloop()
運行結果:
例子
:圖像與文字共存時,文字覆蓋在圖像上方
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, bitmap="error",
compound="center", text="我很喜歡寫博客")
label.pack()
root.mainloop()
運行結果:
11、Widget 的共同屬性 relief
這個屬性也可以應該在許多Widget 控件上面,可以利用relief屬性建立Widget 控件的邊框
參數 | 含義 |
---|---|
flat | |
groove | |
raised | |
ridge | |
solid | |
sunken |
例子
:建立raised
屬性的標籤
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="raised",
relief="raised")
label.pack()
root.mainloop()
運行結果:
12、標籤文字與標籤區間的間距 padx/pady
在設計標籤或其他控件時,若是不設置Widget 的大小,系統將使用最適空間作爲此Widget 的大小。
其實也可以通過設置標籤文字與標籤區間的間距,達到更改標籤區間的目的
- padx:可以設置標籤文字左右邊界與標籤區間的
x
軸間距。 - pady:可以設置標籤文字上下邊界與標籤區間的
y
軸距離。
例子
:
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="raised",
relief="raised",
bg="lightyellow",
padx=5, pady=10)
label.pack()
root.mainloop()
運行結果:
13、圖像 PhotoImage
圖片可以應用在許多地方,例如標籤、功能按鈕、選項按鈕、文字區域等。在使用前可以用PhotoImage()
方法建立圖像對象,然後再將此對象應用在其他窗口組件上。
語法:
# 拓展名gif,傳回圖像對象
imageobj = tkinter.PhotoImage(file="xxx.gif")
需留意這個方法,早期只支持gif
文件格式,不接受常用的jpg
或者png
文件格式的圖像。但是目前已經可以支持png
格式了。爲了使用方便,建議將gif
放在程序所在的文件夾裏。
可以在Label()方法內使用“image=imageobj”參數設置此圖像對象
例子
:
import tkinter
# 創建主窗口
root = tkinter.Tk()
imageobj = tkinter.PhotoImage(file="1.gif")
label = tkinter.Label(root, image=imageobj)
label.pack()
root.mainloop()
運行結果:
當然,gif
和png
是都支持的,同樣,如果需要顯示jpg
的話,我們需要藉助PIL模塊
先安裝pillow模塊
pip install pillow
注意在程序設計中需導入的是PIL模塊
,主要原因是向舊版Python Image Library兼容
from PIL import Image, ImageTk
例子
:在標籤內顯示jpg
文件
import tkinter
from PIL import Image, ImageTk
# 創建主窗口
root = tkinter.Tk()
image = Image.open("1.jpg")
a = ImageTk.PhotoImage(image)
label = tkinter.Label(root, image=a)
label.pack()
root.mainloop()
運行結果:
13-1、可以使用compound
參數是圖像與文字標籤共存
例子
:窗口內同時有文字標籤和圖像的應用
import tkinter
from PIL import Image, ImageTk
# 創建主窗口
root = tkinter.Tk()
text = """學習永不止境,要想有更好的前途,
使勁學習,
成就更好的人生
"""
image = Image.open("1.jpg")
a = ImageTk.PhotoImage(image)
label = tkinter.Label(root, text=text, image=a, bg="lightyellow", compound="left")
label.pack()
root.mainloop()
運行結果:
例子
:使文字在左邊並且靠左輸入
label = tkinter.Label(root, text=text, image=a, bg="lightyellow", compound="right", justify="left")
運行結果:
例子
:圖像與文字共存,文字覆蓋在圖像上方
label = tkinter.Label(root, text=text, image=a, bg="lightyellow", compound="center")
運行結果:
注
:bitmap
與image
是不能共存的,如果發生看了這種情況,bitmap
參數將不起作用
14、Widget 的共同方法 config()
Widget 控件在建立時可以直接設置對象屬性,若是部分屬性未建立,未來在程序執行時,如果想要建立或是更改屬性,可以使用config()
方法。此方法類屬性設置的參數用法與建立時相同。
例子
:添加一個text
內容,並每秒更新一次數據
import tkinter
from PIL import Image, ImageTk
count = 0
def run(test):
def counting():
global count
count += 1
# 添加屬性
test.config(text=str(count))
# 間隔一千毫秒後調用counting函數
test.after(1000, counting)
counting()
# 創建主窗口
root = tkinter.Tk()
image = Image.open("1.jpg")
a = ImageTk.PhotoImage(image)
label = tkinter.Label(root, image=a, bg="lightyellow", compound="center",
font="Heelvetic 20 bold")
label.pack()
run(label)
root.mainloop()
運行結果:
15、Widget 的共同屬性 Cursors
Cursors表示光標的形狀,程序設計時,如果想要更改光標形狀,可以使用此功能
光標形狀可能會因爲操作系統不同而有所差異
例子
:
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="abc", bg="lightyellow",
font="Heelvetic 20 bold",
cursor="heart")
label.pack()
root.mainloop()
運行結果:
16、Widget 的共同方法 keys()
之前介紹過Label()
方法的所有參數,但是Widget
有一個共同方法keys()
可以用列表(list)
傳回這個Widget
所有的參數。
例子
:傳回標籤Label()方法中所有的參數。
import tkinter
# 創建主窗口
root = tkinter.Tk()
label = tkinter.Label(root, text="abc")
label.pack()
print(label.keys())
root.mainloop()
運行結果:
['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']
17、分割線 Separator
我們在設計GUI程序時,有時適度的在適當位置增加分割線可以讓整體視覺效果更佳
tkinter.ttk
中有個Separator模塊
,可以幫助我們增加分割線
Separator(父對象, options)
參數
如下:
- HORIZONTAL:建立水平分割線
- VERTICAL:建立垂直分割線
例子
:
import tkinter
from tkinter.ttk import Separator
# 創建主窗口
root = tkinter.Tk()
text1 = "加油!tkinter"
text2 = """說實話,學編程真的太難了,
要不斷的努力,我也希望我會成功,
大家都會成功的
"""
label = tkinter.Label(root, text=text1,
font="Helvetic 20 bold")
label.pack(padx=10, pady=10)
sep = Separator(root, orient=tkinter.HORIZONTAL)
# pack(fill=tkinter.X, padx=5), 表示此分割線填滿X軸, 它與窗口邊界左右均相距5像素
sep.pack(fill=tkinter.X, padx=5)
label2 = tkinter.Label(root, text=text2)
label2.pack(padx=10, pady=10)
root.mainloop()
運行結果:
是不是看起來很美觀??
謝謝觀看,筆者會持續更新,如有錯誤或者建議,請私信我