目录

1.常用参数

2.共同属性

2.1Color

2.2Dimensions

2.3Anchor

2.4Wraplength

2.5Font

2.6Justify

2.7Bitmaps

 2.8Compound参数

2.9Relief

2.10Padx/Pady

2.11PhotoImage

2.12Config

2.13Cursors

2.14Keys

2.15Separator


1.常用参数

Label()方法可以用于在窗口内建立文字或图像标签,第一个参数是父对象,表示这个标签将建立在哪一个父对象(窗口)内。常用的options参数:

anchor

如果空间大于所需时,控制标签位置,默认是CENTER(居中)

bg/fg

背景/前景色彩

bitmap

使用默认图标当作标签内容

borderwidth

标签宽度,默认是1

compound

可以设置标签内含图像和文字时,彼此的位置关系

cursor

当鼠标光标在标签上方时的外形

font

可选择字形、样式与大小


height/width

标签高度/宽度,单位是字符

image

标签以图像方式呈现

justify

存在多行文本时最后一行的对齐方式,默认是居中CENTER对齐

padx/pady

标签文字与标签区间的间距,单位是像素

text

标签内容,如果有\n则可以输入多行文字

relief

默认relief=FLAT,可由此控制标签的外框

textvariable

可以设置标签以变量方式显示

underline

可以设置第几个文字有下划线,从0开始算起,默认是-1,表示为下划线

wraplength

文本到多少宽度后换行,单位是像素

from tkinter import *

root = Tk()
label = Label(root, text = "Hello world")

label.pack()
root.mainloop()

Python 网页标签获取不了 python 标签内容_html

 运行代码后,窗口是默认大小,tkinter只会安排足够的空间显示控件

2.共同属性

2.1Color

fg/bg可以设置前景色彩和背景色彩

label = Label(root, text = "Hello world",
	bg = "red", fg = "blue")

Python 网页标签获取不了 python 标签内容_前端_02

2.2Dimensions

height/width控制标签的高度和宽度

label = Label(root, text = "Hello world",
	bg = "red", fg = "blue",
	height = 10, width = 20)

Python 网页标签获取不了 python 标签内容_前端_03

 

Python 网页标签获取不了 python 标签内容_html_04

左边图片为默认大小,右边图片为窗口拉伸后的图片

2.3Anchor

Anchor是指标签文字在标签区域输出位置的设置,默认是CENTRE,还可以使用其他参数设置对齐方式

Python 网页标签获取不了 python 标签内容_Python 网页标签获取不了_05

label = Label(root, text = "Hello world",
	bg = "red", fg = "blue",
	height = 10, width = 20,
	anchor = SW)

 

Python 网页标签获取不了 python 标签内容_前端_06

2.4Wraplength

这个参数可以设置标签中的文字在多少宽度后自动换行

label = Label(root, text = "Hello world",
	bg = "red", fg = "blue",
	height = 10, width = 20,
	anchor = NW,
	wraplength = 20)

Python 网页标签获取不了 python 标签内容_python_07

2.5Font

用于设置文字字形,包含下列内容:

(1)字形family:如”黑体”

(2)字号size:单位是像素

(3)weight:bold、normal

(4)slant:italic、roman(是否斜体)

(5)underline:True、False

(6)overstrike:True、False

label = Label(root, text = "Hello world",
	bg = "red", fg = "blue",
	height = 10, width = 20,
	font = "宋体 20 bold italic",
	underline = 5)

Python 网页标签获取不了 python 标签内容_Python 网页标签获取不了_08

2.6Justify

在标签的输出中,如果是多行的输出,在最后一行输出时可以使用justify参数设置所输出的标签内容有LEFT/CENTER/RIGHT,默认是居中输出

label = Label(root, text = "abcdefghijklmnopqrst",
	bg = "lightyellow", fg = "blue",
	height = 10, width = 20,
	wraplength = 50,
	justify = LEFT)

 

Python 网页标签获取不了 python 标签内容_前端_09

Python 网页标签获取不了 python 标签内容_python_10

label1= Label(root, bitmap = "error")
label2= Label(root, bitmap = "warning")
label3= Label(root, bitmap = "question")

左边图是没加justify = LEFT的效果,默认最后一行居中对齐,加了justify = LEFT后最后一行左对齐

2.7Bitmaps

tkinter也提供了在标签位置放置内建位图的功能。下面是在各操作平台都可以使用的位图

error

hourglass

info

questhead

question

warning

gray12

gray25

gray50

gray75

label1= Label(root, bitmap = "error")
label2= Label(root, bitmap = "warning")
label3= Label(root, bitmap = "question")

Python 网页标签获取不了 python 标签内容_html_11

 2.8Compound参数

图像与文字共存时,可以使用此参数定义与文字与图像的位置关系。compound参数可以是下列值:LEFT、RIGHT、TOP、BOTTOM、CENTER

label= Label(root, bitmap = "question",
	compound = LEFT, text = "问题")

label2= Label(root, bitmap = "warning",
	compound = BOTTOM, text = "警告")

Python 网页标签获取不了 python 标签内容_css_12

图像与文字共存时,图像在文字左边/底部

2.9Relief

利用relief属性建立控件的边框

label = Label(root, text = "name", relief = "raised")
label2= Label(root,	text = "age", relief = "flat")

Python 网页标签获取不了 python 标签内容_前端_13

2.10Padx/Pady

在设计标签或其他控件时,若不是设置控件的大小,系统将使用最适空间作为此控件的大小。还可以通过设置标签文字与标签区间的间距,达到更改标签区间的目的。padx可以设置标签文字左右边界与标签区间的x轴间距,pady可以设置标签文字上下边界与标签区间的y轴间距。

label = Label(root, text = "name", relief = "raised",
	bg = "lightyellow",
	padx = 10, pady = 20)

Python 网页标签获取不了 python 标签内容_html_14

2.11PhotoImage

图片可以应用在许多地方,例如标签、功能按钮、选项按钮、文字区域等。在使用前可以用PhotoImage()方法建立图像对象,然后再将此对象应用在其他窗口组件上。语法如下:

imagelabel = PhotoImage(file = "xxx.gif")

需留意PhotoImage()方法早期只支持gif格式,不接受常用的jpg格式或png格式的图像,目前已经可以支持png格式了。为了使用方便建议将gif图片放在程序所在文件夹中。

imagelabel = PhotoImage(file = "stop4.png")
label = Label(root, image = imagelabel)

Python 网页标签获取不了 python 标签内容_前端_15

 如果想要在标签内显示jpg文件,需要借助PIL模块的Image和ImageTk模块,需要先导入pillow模块

pip install pillow

注意在程序设计中需导入的是PIL模块,主要原因是要向旧版Python Image Library兼容

from tkinter import *
from PIL import Image, ImageTk

root = Tk()
root.geometry("500x500")
image = Image.open("stop4.jpg")
stop4 = ImageTk.PhotoImage(image)
label = Label(root, image = stop4)

label.pack()
root.mainloop()

Python 网页标签获取不了 python 标签内容_css_16

图片尺寸是500x500,部分图片没有完全显示出来,经窗口拉伸后可以完全显示

当窗口内同时有文字标签和图像时:

text1 = "这是一张“停止”图片"
stop4 = PhotoImage(file = "stop4.png")
label = Label(root, image = stop4, text = text1,
	bg = "lightyellow", compound = BOTTOM)

Python 网页标签获取不了 python 标签内容_Python 网页标签获取不了_17

如果文本较长,可以用justify参数来控制末尾对齐方式。此外,bitmap参数和image参数不能共存,如果发生这种状况,bitmap参数将不起作用。

2.12Config

控件在建立时可以直接设置对象属性,若是部分属性未建立,未来在程序执行时如果想要建立或更改属性可以用config()方法。才方法内属性设置的参数用法与建立时相同。

2.13Cursors

Cursors表示光标形状。可以用来设计鼠标光标在标签或按钮上时的形状。

label = Label(root, image = stop4, text = text1,
	bg = "lightyellow", compound = BOTTOM,
	cursor = "plus")

Python 网页标签获取不了 python 标签内容_python_18

Python 网页标签获取不了 python 标签内容_python_19

当鼠标经过此标签时,其形状变为“plus”。

2.14Keys

控件有一个共同方法keys()可以用列表传回这个控件的所有的参数

print(label.keys())

Python 网页标签获取不了 python 标签内容_前端_20

2.15Separator

在设计GUI程序时,有时适度地在适当位置添加分割线可以让整体视觉效果更加。tkinter.ttk中有Separator模块,可以完成此工作。

from tkinter import *
from tkinter.ttk import Separator

root = Tk()
title = "这是主题"
body = "这是内容"
label = Label(root, text = title, font = "黑体 20 bold")
label.pack()

sep = Separator(root, orient = HORIZONTAL)
sep.pack(fill = X, padx = 10)

label2 = Label(root, text = body)
label2.pack(padx = 50, pady = 50)

root.mainloop()

Python 网页标签获取不了 python 标签内容_前端_21

参考文献:Python GUI设计——tkinter菜鸟编程,洪锦魁著