文章目錄
- 簡單介紹
- 使用Label控件
-
- Label參數
-
- activebackground, activeforeground, disabledforeground與state的使用
- bg, bd, height, width與anchor的使用
- 文字與圖像的顯示(bitmap, image)
-
- bitmap圖示顯示
- image圖像顯示
- font設定文體和字型大小
- justify和wraplength的使用
- textvariable
簡單介紹
Label控件可以叫标簽,用于顯示文本或圖像。
使用Label控件
Label參數
參數名 | 描述 | 參數值 |
---|---|---|
activebackground | 标簽激活時候的背景顔色(激活狀态由下面的state參數控制) | 顔色英文單詞 |
activeforeground | 标簽激活時候的文字和圖像顔色(激活狀态由下面的state參數控制) | 顔色英文單詞 |
anchor | 文本或圖像在Label的位置 | ‘n’, ‘s’, ‘e’, ‘w’, ‘nw’, ‘sw’, ‘se’, ‘ne’, ‘center’(預設為’center’) |
background、bg | 标簽的背景色 | 顔色的英文,如’blue’ |
bitmap | 加入位圖 | 自帶的有error,hourglass,info,questhead,warning,gray12,gray25,gray50,gray75, 使用者可以自定義 |
borderwidth、bd | 設定邊框寬度 | 自然數 |
compound | 控制文本與圖像的顯示狀态,預設情況下,有圖不顯示文字 | bottom, center, left, none, right, top, 預設值為none |
cursor | 設定滑鼠移動到标簽時的形狀 | arrow, circle, clock, cross, dotbox, exchange, fleur, heart, man, mouse, pirate, plus, shuttle, sizing, spider, spraycan, star, target, tcross, trek, watch |
disabledforeground | 指定當 Label 不可用的時候前景色的顔色 | 顔色英文單詞 |
font | 設定标簽文本字型 | 字型名稱 |
foreground、fg | 設定文本與圖像顔色 | 顔色英文單詞 |
height、width | 設定标簽的高寬。顯示的是文本,那麼機關是文本單元;顯示的是圖像,那麼機關是像素 | 自然數 |
highlightbackground、highlightcolor、highlightthickness | 沒有獲得焦點的時候高亮邊框的顔色、獲得焦點的時候高亮邊框的顔色、高亮邊框的寬度 | |
image | 加入圖像 | tkinter的PhotoImage的執行個體化對象 |
justify | 多行文本對齊方式 | center,left, right |
padx、pady | 水準、垂直方向的邊距 | 自然數 |
relief | 控件樣式 | flat, groove, raised, ridge, solid, sunken |
state | 設定元件狀态 | active, disable, normal |
takefocus | 設定Label是否接受輸入焦點 | |
text | 用于在标簽上顯示文本内容 | 字元串 |
textvariable | 一個StringVar變量,Label顯示該變量内容。如果變量被修改,Label會自動更新文本 | StringVar變量 |
underline | 隻能指定一個字元有下劃線(例如用于表示鍵盤快捷鍵) | 自然數 |
wraplength | 指定文本有多少寬度後開始換行,機關是螢幕單元 | 自然數 |
沒寫參數值的參數是我暫時還無法搞清楚,無法實作的。
activebackground, activeforeground, disabledforeground與state的使用
import tkinter as tk
root = tk.Tk()
lab1 = tk.Label(root, text="這是夢醒時候的代碼*****Label1",
disabledforeground='yellow',
activeforeground='green',
activebackground = 'white',
state='disabled')
lab2 = tk.Label(root, text="這是夢醒時候的代碼----Label2",
disabledforeground='yellow',
activeforeground='green',
activebackground = 'white',
state='active')
lab1.pack()
lab2.pack()
root.mainloop()
bg, bd, height, width與anchor的使用
import tkinter as tk
root = tk.Tk()
lab1 = tk.Label(root, text="這是夢醒時候的代碼*****Label1",
bg='red',
height=20,
width=50,
anchor='w')
lab2 = tk.Label(root, text="這是夢醒時候的代碼----Label2",
bg='red',
bd=10,
height=20,
width=50,
anchor='w'
)
lab1.pack()
lab2.pack()
root.mainloop()
文字與圖像的顯示(bitmap, image)
bitmap圖示顯示
import tkinter as tk
root = tk.Tk()
# height與width的機關為像素
lab1 = tk.Label(root, text="這是夢醒時候的代碼*****Label1",
bg='red',
height=100,
width=200,
bitmap = 'error',
compound='top')
# height與width的機關為文本單元
lab2 = tk.Label(root, text="這是夢醒時候的代碼----Label2",
bg='red',
height=20,
width=50)
# fg改變圖像與文字的顔色
lab3 = tk.Label(root, text="這是夢醒時候的代碼*****Label1",
bg='red',
height=100,
width=200,
bitmap = 'error',
compound='top',
fg = 'blue')
lab1.pack()
lab2.pack()
lab3.pack()
root.mainloop()
image圖像顯示
import tkinter as tk
root = tk.Tk()
# PhotoImage隻能讀取gif類型的圖像
img = tk.PhotoImage(file = 'D:/Documents/Desktop/2.jpg')
lab = tk.Label(root, image=img)
lab.pack()
root.mainloop()
font設定文體和字型大小
如何檢視python已有字型?
Options->Configure IDLE
import tkinter as tk
root = tk.Tk()
tk.Label(root, text="這是夢醒時候的代碼*****Label1", font = '幼圓').pack()
tk.Label(root, text="這是夢醒時候的代碼*****Label1", font = '華文隸書').pack()
# 設定字型大小
tk.Label(root, text="這是夢醒時候的代碼*****Label1", font = ('華文隸書', 20)).pack()
root.mainloop()
justify和wraplength的使用
import tkinter as tk
root = tk.Tk()
tk.Label(root, text='這是夢醒時候的代碼*****Label1', bg='red').pack()
tk.Label(root, text='這是夢醒時候的代碼*****Label2', bg='yellow', justify='left', wraplength=100).pack()
tk.Label(root, text='這是夢醒時候的代碼*****Label3', bg='blue', justify='center', wraplength=100).pack()
root.mainloop()
textvariable
import tkinter as tk
root = tk.Tk()
content = tk.StringVar()
content.set('夢醒時候')
def func():
content.set('你好')
tk.Label(root, textvariable=content).pack()
tk.Button(root, text='改變', command=func).pack()
root.mainloop()
點選按鈕,執行func方法,即把content的值改為
你好
,那麼Label顯示的文本内容也随之改變。即可以動态改變文本内容。
上一篇:認識控件與幾何管理