Python的GUI学习笔记——Tkinter笔记 【三】Label控件

本文详细介绍了Tkinter中Label控件的使用方法及其参数配置,包括文本与图像的显示、样式调整等,并通过示例代码展示了不同参数的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单介绍

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显示的文本内容也随之改变。即可以动态改变文本内容。


上一篇:认识控件与几何管理

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值