一、参数说明
| 语法 | 作用 |
|---|---|
| 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()
三、效果图
