文章目录
简单介绍
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显示的文本内容也随之改变。即可以动态改变文本内容。
上一篇:认识控件与几何管理