一、參數說明
| 語法 | 作用 |
|---|---|
| Label(window,text=‘xxxxx’) | 需要在界面顯示的Label標簽內容 |
| Label(window,text=‘xxxxx’,height=2) | 組件的高度(所占行數) |
| Label(root,text=‘xxxxx’,height=2,width=20) | 組件的寬度(所占字符個數) |
| Label(root,text=‘xxxxx’,fg='blue') | 顯示字體為藍色 |
| Label(root,text=‘xxxxx’,bg=‘red’) | 顯示背景為紅色 |
| Label(root,text=‘xxxxx’,justify=tk.LEFT) | 多行文本的對齊方式 |
| Label(root,text=‘xxxxx’,padx=5) | 文本左右兩側的空格數 |
| Label(root,text=‘xxxxx’,pady=5) | 文本上下兩側的空格數 |
| Label(root,text=‘xxxxx’,font=("微軟雅黑", 12)) | 設置字體格式和大小 |
| Label(image=tk.PhotoImage(file="./image/loading.gif")) | 設置背景圖片 |
| Label(root,text=‘xxxxx’,image=photo,compound=tk.CENTER) | 圖像背景圖位置 |
| Label(root,text=‘xxxxx’,anchor=tk.W) | 設置文字位置 |
二、代碼示例
import tkinter as tk
window = tk.Tk()
def main():
global window
# 設置主窗體大小
winWidth = 600
winHeight = 400
# 獲取屏幕分辨率
screenWidth = window.winfo_screenwidth()
screenHeight = window.winfo_screenheight()
# 計算主窗口在屏幕上的坐標
x = int((screenWidth - winWidth)/ 2)
y = int((screenHeight - winHeight) / 2)
# 設置主窗口標題
window.title("Label參數說明")
# 設置主窗口大小
window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
# 設置窗口寬高固定
window.resizable(0,0)
# 設置窗口圖標
window.iconbitmap("./image/icon.ico")
"""參數列表
STANDARD OPTIONS
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
"""
# 只能傳gif圖片
# cusor : wait
photo = tk.PhotoImage(file="./image/loading.gif")
label = tk.Label(window, text="正在加載...", width=100,
font=("Arial", 12),justify=tk.LEFT,
bg="#ccc", fg="#f00", pady=10,
image=photo, compound=tk.TOP,
anchor="w", cursor="")
label.pack()
window.mainloop()
if __name__ == '__main__':
main()
三、效果圖

