目录



  • 目录
  • 前言
  • (一)基本用法和可选属性
  • ==1.基本用法==
  • ==2.可选属性==
  • (二)属性的具体使用
  • ==1.常用属性==
  • ==2.边距与文本对齐方式==
  • ==案例一==
  • ==案例一的效果==
  • ==3.Label里放图片==
  • ==案例二==
  • ==案例二的效果==
  • ==案例三==
  • ==案例三的效果==
  • ==4.Label可变的文字内容==
  • ==案例四==
  • ==案例四的效果==
  • ==4.文字所在标签框的方位==
  • ==案例五==
  • ==案例五的效果==
  • ==5.边框样式与宽度==
  • ==案例六==
  • ==案例六的效果==



目录

前言

Label是用于显示文本或图像的控件。

(一)基本用法和可选属性

==1.基本用法==

基本用法:Label(根对象, [属性列表])

根对象:在那个窗体显示,例如主窗体。

属性列表:是可选的属性=属性值组成。

==2.可选属性==

属性

说明

text

标签显示的文本

font

设置文本的字体和大小

fg(foreground)

字体的颜色,

bg (background)

标签的背景色

width

标签的宽度(一个中文的字体宽为单位)

height

标签的高度(一个中文的字体高为单位)

padx

文字到边框的距离,水平方向

pady

文字到边框的距离,垂直方向

bd(borderwidth)

边框的宽度

relief

边框的样式

justify

文本对齐方式

image

图片

compound

图片与文字的混搭

anchor

方位

(二)属性的具体使用

==1.常用属性==

(1)font

font:设置字体与字体的大小

用法:font=("字体名",大小) 例如:font=(“黑体”, 20)

(2)fg 与 bg

fg 前景色,也就是字体的颜色,bg 背景颜色

用法:fg="red", fg="#121234"

(3)width 与 height

width height 标签的宽度与高度,都是以系统默认的中文的一个字体宽高为单位

用法:width = 5, height=2

==2.边距与文本对齐方式==

(1)边距:padx 与 pady

文字到边框的距离 padx 与 pady 水平与竖直方向

用法:padx=2, pady = 1 ,单位是像素,默认是1像素

(2)文本对齐方式:justify

Label 里的文本支持 转移字符 \n \t 等

用法:justify="left" ,可取 "left" ,"right", "center" ,默认是"center"居中对齐

==案例一==

import tkinter as tk

# 背景色与前景色
win = tk.Tk()
label_1 = tk.Label(win, text='fg="red" bg="blue"', fg="red", bg="blue")
label_1.pack()

# 字体与字体的大小
label_2 = tk.Label(win, text='font=("微软雅黑", 20)', font=("微软雅黑", 20))
label_2.pack()

# 宽度与高度
label_3 = tk.Label(win, text='width=10, height=3', bg="yellow", width=10, height=3)
label_3.pack()
label_4 = tk.Label(win, text='width=20, height=1', bg="yellow", width=20, height=1)
label_4.pack()

# 边距
label_5 = tk.Label(win, text='padx pady 默认', bg="blue")
label_5.pack()
label_6 = tk.Label(win, text='padx=0, pady=0', bg="blue", padx=0, pady=0)
label_6.pack()
label_7 = tk.Label(win, text='padx=10, pady=10', bg="blue", padx=10, pady=10)
label_7.pack()

# 对齐方式
label_8 = tk.Label(win, text="我是张一根,一匹想飞的狼,\n999号哦")
label_8.pack()
label_9 = tk.Label(win, text="我是张一根,一匹想飞的狼,\n999号哦", justify="left")
label_9.pack()
label_9 = tk.Label(win, text="我是张一根,一匹想飞的狼,\n999号哦", justify="right")
label_9.pack()

win.mainloop()
import tkinter as tk

# 背景色与前景色
win = tk.Tk()
label_1 = tk.Label(win, text='fg="red" bg="blue"', fg="red", bg="blue")
label_1.pack()

# 字体与字体的大小
label_2 = tk.Label(win, text='font=("微软雅黑", 20)', font=("微软雅黑", 20))
label_2.pack()

# 宽度与高度
label_3 = tk.Label(win, text='width=10, height=3', bg="yellow", width=10, height=3)
label_3.pack()
label_4 = tk.Label(win, text='width=20, height=1', bg="yellow", width=20, height=1)
label_4.pack()

# 边距
label_5 = tk.Label(win, text='padx pady 默认', bg="blue")
label_5.pack()
label_6 = tk.Label(win, text='padx=0, pady=0', bg="blue", padx=0, pady=0)
label_6.pack()
label_7 = tk.Label(win, text='padx=10, pady=10', bg="blue", padx=10, pady=10)
label_7.pack()

# 对齐方式
label_8 = tk.Label(win, text="我是张一根,一匹想飞的狼,\n999号哦")
label_8.pack()
label_9 = tk.Label(win, text="我是张一根,一匹想飞的狼,\n999号哦", justify="left")
label_9.pack()
label_9 = tk.Label(win, text="我是张一根,一匹想飞的狼,\n999号哦", justify="right")
label_9.pack()

win.mainloop()

==案例一的效果==

==3.Label里放图片==

(1)只放图片,没有文字

需要先导入图片的路径:img1 = tk.PhotoImage(file="image/01.png")

再使用:image=img1

注:目前支持 .png 与 .gif 格式, 还不支持 .jpg格式

==案例二==

import tkinter as tk

win = tk.Tk()

# 导入图片
img1 = tk.PhotoImage(file="image/01.png")

# 在标签里放入图片
label_image1 = tk.Label(win, image=img1)
label_image1.pack()

win.mainloop()
import tkinter as tk

win = tk.Tk()

# 导入图片
img1 = tk.PhotoImage(file="image/01.png")

# 在标签里放入图片
label_image1 = tk.Label(win, image=img1)
label_image1.pack()

win.mainloop()

==案例二的效果==

(2)图片与文字混搭

需要使用:compound="对齐方式",

对齐方式有:'left', "right", "center"

==案例三==

import tkinter as tk

win = tk.Tk()

# 导入图片
img1 = tk.PhotoImage(file="image/01.png")

# 在标签里放入图片
label_image1 = tk.Label(win, text="图文并茂", image=img1, compound="left")
label_image1.pack()

label_image2 = tk.Label(win, text="图文并茂", image=img1, compound="right")
label_image2.pack()

label_image3 = tk.Label(win, text="图文并茂", image=img1, compound="center", fg="red")
label_image3.pack()

win.mainloop()
import tkinter as tk

win = tk.Tk()

# 导入图片
img1 = tk.PhotoImage(file="image/01.png")

# 在标签里放入图片
label_image1 = tk.Label(win, text="图文并茂", image=img1, compound="left")
label_image1.pack()

label_image2 = tk.Label(win, text="图文并茂", image=img1, compound="right")
label_image2.pack()

label_image3 = tk.Label(win, text="图文并茂", image=img1, compound="center", fg="red")
label_image3.pack()

win.mainloop()

==案例三的效果==

==4.Label可变的文字内容==

先定义一个:tk变量,v = tk.StringVar()

在使用:textvariable=v

==案例四==

import tkinter as tk
import time

# 背景色与前景色
win = tk.Tk()

# 定义变量,必须是使用的是tk里的这个变量
v = tk.StringVar()

# 使用v.set("string") 来定义变量
v.set("这是刚开始的文字")

# 在标签里添加可变的文本
label_1 = tk.Label(win, textvariable=v, fg="red", bg="green", font=("黑体", 40))
label_1.pack()
while True:
    v.set("%s" % time.ctime())
    label_1.update()
    time.sleep(1)

win.mainloop()
import tkinter as tk
import time

# 背景色与前景色
win = tk.Tk()

# 定义变量,必须是使用的是tk里的这个变量
v = tk.StringVar()

# 使用v.set("string") 来定义变量
v.set("这是刚开始的文字")

# 在标签里添加可变的文本
label_1 = tk.Label(win, textvariable=v, fg="red", bg="green", font=("黑体", 40))
label_1.pack()
while True:
    v.set("%s" % time.ctime())
    label_1.update()
    time.sleep(1)

win.mainloop()

==案例四的效果==

可以制作一个时间表

==4.文字所在标签框的方位==

anchor 可选的参数有:"e","w","s","n","ne","se","nw","sw",就是东西南北八个方向

anchor="n"

==案例五==

import tkinter as tk

win = tk.Tk()
label_1 = tk.Label(win, text="方位是 N", bg="green", width=10, height=3, anchor="n")
label_1.pack()

label_2 = tk.Label(win, text="方位是 NE", bg="blue", width=10, height=3, anchor="ne")
label_2.pack()

label_3 = tk.Label(win, text="方位是 SW", bg="red", width=10, height=3, anchor=tk.SW)
label_3.pack()

label_4 = tk.Label(win, text="方位是 W", bg="yellow", width=10, height=3, anchor=tk.W)
label_4.pack()
win.mainloop()
import tkinter as tk

win = tk.Tk()
label_1 = tk.Label(win, text="方位是 N", bg="green", width=10, height=3, anchor="n")
label_1.pack()

label_2 = tk.Label(win, text="方位是 NE", bg="blue", width=10, height=3, anchor="ne")
label_2.pack()

label_3 = tk.Label(win, text="方位是 SW", bg="red", width=10, height=3, anchor=tk.SW)
label_3.pack()

label_4 = tk.Label(win, text="方位是 W", bg="yellow", width=10, height=3, anchor=tk.W)
label_4.pack()
win.mainloop()

==案例五的效果==

==5.边框样式与宽度==

relief 表示边框样式,可选的参数有:flat(平的),sunken (沉没的, 凹下去的),raised (提高,凸出来的),ridge(脊,中键凸的)

relief="sunken", 默认是flat.

==案例六==

import tkinter as tk

win = tk.Tk()


# 在标签里放入图片SUNKEN, RAISED, GROOVE, and RIDGE
# 默认 flat平的
label_1 = tk.Label(win, text='relief=flat', bd=8)
label_1.pack()

# sunken 沉没的
label_2 = tk.Label(win, text='relief=sunken', relief="sunken", bd=5)
label_2.pack()

# raised 提高
label_3 = tk.Label(win, text='relief=raised', relief="raised", bd=5)
label_3.pack()

# ridge脊
label_4 = tk.Label(win, text='relief=ridge', relief="ridge", bd=10)
label_4.pack()

win.mainloop()
import tkinter as tk

win = tk.Tk()


# 在标签里放入图片SUNKEN, RAISED, GROOVE, and RIDGE
# 默认 flat平的
label_1 = tk.Label(win, text='relief=flat', bd=8)
label_1.pack()

# sunken 沉没的
label_2 = tk.Label(win, text='relief=sunken', relief="sunken", bd=5)
label_2.pack()

# raised 提高
label_3 = tk.Label(win, text='relief=raised', relief="raised", bd=5)
label_3.pack()

# ridge脊
label_4 = tk.Label(win, text='relief=ridge', relief="ridge", bd=10)
label_4.pack()

win.mainloop()

==案例六的效果==