tkinter组件详解之Label

tkinter组件详解之Label

Label组件用于在屏幕上显示文本或图像。最红呈现出的结果是由背景和前景叠加构成的。

函数定义:Label(master=None, cnf={}, **kw)

背景

背景由三部分构成:内容、填充区、边框

内容区的参数:width、height,用于指定区域的大小,单位依据前景的具体内容而变化(前景内容是文字--->单位:字符,前景内容是图片--->单位:像素)

填充区的参数:padx、pady,用于指定内容与边框之间的距离,单位:像素

边框区的参数:relief,用于指定边框区的样式( 可选值为:flat(默认),sunken,raised,groove,ridge )

borderwidth:用于指定边框的宽度,单位:像素

下面用一张图来说明背景区:

设置Label的背景属性

import tkinter as tk
win = tk.Tk()
win.title("Label的基本使用")
label = tk.Label(win, text="Label的基本使用", background="red",
                 padx=0, pady=0, borderwidth=10, relief="ridge")
label.pack()
win.mainloop()
import tkinter as tk
win = tk.Tk()
win.title("Label的基本使用")
label = tk.Label(win, text="Label的基本使用", background="red",
                 padx=50, pady=50, borderwidth=10, relief="ridge")
label.pack()
win.mainloop()
代码一 代码二

|

前景

前景分为文本和图片两部分

文本

文本参数:font=(font_name, size),指定字体和字体大小

justify = "center(默认)left/right/" ,指定文本的对齐方式

foreground = "指定的颜色" ,指定文字的颜色

text = "文本内容",指定文本的内容(静态的)

anchor = " n/s/w/e/ne/nw/sw/se/center(默认) ",指定文本在背景内容区的位置(n:北、w:西、e:东、s:南)

设置文本的属性

import tkinter as tk

win = tk.Tk()
win.title("Label的基本使用")
text = """Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!"""
label = tk.Label(win, text=text, background="red",
                 font=("黑体", 16), justify="left", foreground="blue")
label.pack()
win.mainloop()

图片

图片参数: image = normal_image(仅支持GIF, PPM/PGM格式的图片),通常需要从Pillow库中引入Image和ImageTk,使用如下语句来转换为支持的图片格式。

from PIL import Image
from PIL import ImageTk
import tkinter
# imagePath为图片保存的路径
img = Image.open(imagePath)
img = ImageTk.PhotoImage(img)

compound = "bottom/top/left/right/center/None(默认)",bottom/top/left/right表示图片显示在文本的下/上/左/右,center表示文本显示在图片的上面

设置图片属性

import tkinter as tk
import os
from PIL import Image
from PIL import ImageTk

win = tk.Tk()
win.title("Label的基本使用")
print(os.getcwd())
image_path = os.path.join(os.getcwd(), r"image\03.jpg")
img = Image.open(image_path)
img = ImageTk.PhotoImage(img)
label = tk.Label(win, text="Hello Python", font=("黑体", 26), foreground="blue",
                 image=img, compound="center")
label.pack()
win.mainloop()

参数列表

activebackground 设置当 Label 处于活动状态(通过 state 选项设置状态)的背景色
activeforeground 设置当 Label 处于活动状态(通过 state 选项设置状态)的前景色
anchor 控制文本(或图像)在 Label 中显示的位置
background 设置背景颜色
bitmap 指定显示到 Label 上的位图
borderwidth 指定 Label 的边框宽度
cursor 指定当鼠标在 Label 上飘过的时候的鼠标样式
disabledforeground 指定当 Label 不可用的时候前景色的颜色
font 指定 Label 中文本的字体
foreground 设置 Label 的文本和位图的颜色
highlightbackground 指定当 Label 没有获得焦点的时候高亮边框的颜色
highlightcolor 指定当 Label 获得焦点的时候高亮边框的颜色
highlightthickness 指定高亮边框的宽度
image 指定 Label 显示的图片
justify 定义如何对齐多行文本
padx,pady 指定 Label 水平方向,垂直方向上的额外间距
relief 指定边框样式
takefocus 如果是 True,该 Label 接受输入焦点
text 指定 Label 显示的文本
textvariable Label 显示 Tkinter 变量(通常是一个 StringVar 变量)的内容,如果变量被修改,Label 的文本会自动更新
underline 跟 text 选项一起使用,用于指定哪一个字符画下划线
wraplength 决定 Label 的文本应该被分成多少行
height 设置 Label 的高度
state 指定 Label 的状态
width 设置 Label 的宽度

猜你喜欢

转载自www.cnblogs.com/Rnake/p/12304863.html