有的时候,制作安安教具需要在指定位置显示文本或者图片,这时离不开标签Label的使用

一.基本使用

Label控件的基本语法如下:

Label(父对象,参数1,参数2...)

其中父对象可以是窗口,容器,框架,接下来详细介绍参数:

参数名

介绍

anchor

当空间大于所需时的控件设置,可选参数有:n(北),s(南),w(西),e(东),center(中心),nw(西北),ne(东北),sw(西南),se(东南)

bg或background

设置背景颜色

bitmap

设置默认图标作为标签内容

borderwidth或bd

设置标签的边界宽度

compound

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

cursor

当鼠标落在标签上时的显示

fg或foreground

设置前景色彩

font

选择字形,自形样式,大小等

height

设置标签的高度,单位是字符

image

标签中显示图像

justify

存在多行文本时对最后一行的对齐方式,可选值有LEFT、CENTER、RIGHT(左,中,右),默认为居中对齐

padx/pady

设置标签文字与标签区间的间距,以像素为单位

relief

设置标签的外框如何显示

text

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

textvariable

设置标签以变量的方式进行显示

underline

设置标签有多少下划线

width

设置标签的宽度,单位是字符

wraplength

设置文本到多少宽度后换行

二.anchor参数[1]

 

设置该参数,各个选项的意义如下:

字母

方位

n


s


w

西

e


center

中心

nw

西北

ne

东北

sw

西南

se

东南

 

用一张图显示会更明显:

python tkinter Label结果可复制 tkinter中label_源码软件

 下面的代码是一个案例[2]: 

from tkinter import messagebox as box
from tkinter import Label
import tkinter as tk


def main_menu():
    window = tk.Tk()
    window.title('安安教具-anchor演示')
    window.geometry('800x800')
    window.configure(background = 'black')

    label = Label(window, text = 'Label1', fg = 'light green', bg = 'black', font = (None, 30), height = 2)
    label.pack(side = 'top')

    show_label1 = Label(window, text = '标签1', width = 25, height = 2)
    show_label1.pack(pady = 10, padx = 25, anchor = 'w')

    show_label2 = Label(window, text = '标签2', width = 25, height = 2)
    show_label2.pack(pady = 10, padx = 25, anchor = 'w')

    show_label3 = Label(window, text = '标签3', width = 25, height = 2)
    show_label3.pack(pady = 10, padx = 25, anchor = 'w')

    show_label4 = Label(window, text = '标签4', width = 25, height = 2)
    show_label4.pack(pady = 10, padx = 25, anchor = 'w')

    show_label5 = Label(window, text = '标签5', width = 25, height = 2)
    show_label5.pack(pady = 10, padx = 25, anchor = 'w')

    show_label6 = Label(window, text = '标签6', width = 25, height = 2)
    show_label6.pack(pady = 10, padx = 25, anchor = 'w')

    show_label7 = Label(window, text = '标签7', width = 25, height = 2)
    show_label7.pack(pady = 10, padx = 25, anchor = 'n')

    show_label8 = Label(window, text = '标签8', width = 25, height = 2)
    show_label8.pack(pady = 10, padx = 25, anchor = 'n')

    window.mainloop()



main_menu()

 运行效果如下:

python tkinter Label结果可复制 tkinter中label_源码软件_02

 三.bg/background,fg/foreground参数

from tkinter import *
root=Tk()
root.title("安安教具-颜色")
label=Label(root,text="安安教具",fg="#000000",bg="yellow")
label.pack()
root.mainloop()

 运行效果如下:

python tkinter Label结果可复制 tkinter中label_程序人生_03

四.bitmap参数[3]

from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("安安教具-bitmap")
label1 = Label(root, bitmap="error").pack()
label2 = Label(root, bitmap="info").pack()
label3 = Label(root, bitmap="hourglass").pack()
label4 = Label(root, bitmap="questhead").pack()
label5 = Label(root, bitmap="question").pack()
label6 = Label(root, bitmap="warning").pack()
label7 = Label(root, bitmap="gray12").pack()
label8 = Label(root, bitmap="gray25").pack()
label9 = Label(root, bitmap="gray50").pack()
label10 = Label(root, bitmap="gray75").pack()
root.mainloop()

 

运行后效果如下:

python tkinter Label结果可复制 tkinter中label_源码软件_04

 

五.总结

至于其余的参数使用,安安建议大家养成良好的查百度习惯,我就不再介绍啦~


 

参考 

1.【学习日常随记】一分钟搞懂tkinter中的锚点(anchor)问题

 2.在Tkinter中使用Anchorhttps://qa.1r1g.com/sf/ask/2506902611/

 3.Python3 Tkinter 实例教学 (十一)标签Label 使用图片bitmap image