Tkinter (07) 标签部件 Label
标签部件的创建及其选项#
import tkinter as tk
parent = tk.Tk()
w = tk.Label(parent, option, ...)
选项 | 说明 |
---|---|
activebackground | active 时的背景色 |
activeforeground | active 时的前景色 |
anchor | 部件定位点,内定为 CENTER |
bg or background | 背景色,点阵图时则为颜色值 0 的颜色 |
bitmap | 点阵图 |
bd or borderwidth | 框的寛度 |
compound | 图与文本共用时,图相对于文本的位置. LEFT/RIGHT/TOP/BOTTOM/CENTER |
cursor | 当鼠标移到部件时,所显示的鼠标图示 |
disabledforeground | disabled 时的前景色 |
font | 文本字体 |
fg or foreground | 前景色,点阵图时则为颜色值 1 的颜色 |
height | 文本行数 |
highlightbackground | 非聚焦时的聚焦颜色,Win10 下可能有问题,需要另外处理 |
highlightcolor | 聚焦时的聚焦颜色,Win10 下可能有问题,需要另外处理 |
highlightthickness | 聚焦厚度,内定 1, 0 则无 |
image | 图片 |
justify | 多行文本的对齐方式,LEFT/RIGHT/CENTER |
padx | 水平间距,内定为 1 点素 |
pady | 垂直间距,内定为 1 点素 |
relief | 花边样式,内定为 FLAT |
state | 状态 NORMAL 内定 / ACTIVE/DISABLED |
takefocus | TAB 键在部件是否会循环焦点,1 则会,内定为空字符串,则仅在具有键绑定下循环焦点 |
text | 文本字串,使用 \n 分行 |
textvariable | 文本字串变量 |
underline | 设置下底线的位置,内定 -1 为无 |
width | 文本寛度,内定为显示文本或图片的尺寸 |
wraplength | 文本分行寛度,單位為点素 |
范例视窗及代码#
import tkinter as tk
root = tk.Tk()
root.wm_title("Label Demo")
t = ("Python is an easy to learn, powerful programming language. It has "
"efficient high-level data structures and a simple but effective "
"approach to object-oriented programming. Python’s elegant syntax and "
"dynamic typing, together with its interpreted nature, make it an ideal "
"language for scripting and rapid application development in many areas "
"on most platforms.")
label_1 = tk.Label(root, text=t, width=50, wraplength=400, height=10,
anchor=tk.NW, justify=tk.LEFT, borderwidth=5, relief=tk.SOLID)
label_1.grid(row=0, column=0)
label_2 = tk.Label(root, text=t, width=50, wraplength=400, height=10,
anchor=tk.SE, justify=tk.RIGHT, borderwidth=5)
label_2.grid(row=1, column=0)
root.mainloop()
本作品采用《CC 协议》,转载必须注明作者和本文链接