教程 > tkinter 教程 > 阅读:312

tkinter 教程 标签控件——迹忆客-ag捕鱼王app官网

label 标签控件通过用来显示静态的文本或者图像,它的内容一般来说不是动态的。当然,你可以根据你的需求来改变它的内容。

from sys import version_info
if version_info.major == 2:
    import tkinter as tk
elif version_info.major == 3:
    import tkinter as tk
    
app = tk.tk()
labelexample = tk.label(app, text="this is a label")
labelexample.pack()
app.mainloop()

程序运行后,会在主程序窗口内生成一个文字标签。

tkinter basic label

labelexample = tk.label(app, text = "this is a label")

labelexample 是在主窗口 app 内的一个显示 this is a label 的标签实例。

labelexample.pack()

pack() 方法用来对主控件里面的小控件来进行布局分布。它有如下的参数列表,

pack() 方法 描述
after=widget 打包widget后打包
anchor=nsew (or subset) position widget according to
before=widget 在打包小部件之前打包它
expand=bool 如果父级大小增加则扩展小部件
fill=none or x or y or both 如果小部件增长,则填充小部件
in=master 使用 master 来包含这个小部件
in_=master 请参阅“in”选项说明
ipadx=amount 在x方向添加内部填充
ipady=amount 在y方向添加内部填充
padx=amount 在x方向添加填充
pady=amount 在y方向添加填充
side=top or bottom or left or right 在哪里添加这个小部件。

通过更改 pack() 的参数,你可以获取不同的控件布局。

标签的尺寸是由属于标签选项中的宽度和高度来定义的。

备注 标签内包含文字或者图片的时候,它们的宽度及高度的单位是不同的。如果是文字的话,那单位就是单个字符的宽度;如果是图片的话,那单位就是像素了。

工欲善其事必先利其器,我们可以先来看看标签对象里面都有哪些属性 options。通过 dict(label) 我们可以列出标签对象的所有属性。

from sys import version_info
if version_info.major == 2:
    import tkinter as tk
elif version_info.major == 3:
    import tkinter as tk
    
from pprint import pprint
    
app = tk.tk()
labelexample = tk.label(app, text="this is a label", height=15, width=100)
pprint(dict(labelexample))

得到的属性字典如下,

{'activebackground': 'systembuttonface',
 'activeforeground': 'systembuttontext',
 'anchor': 'center',
 'background': 'systembuttonface',
 'bd': ,
 'bg': 'systembuttonface',
 'bitmap': '',
 'borderwidth': ,
 'compound': 'none',
 'cursor': '',
 'disabledforeground': 'systemdisabledtext',
 'fg': 'systembuttontext',
 'font': 'tkdefaultfont',
 'foreground': 'systembuttontext',
 'height': 15,
 'highlightbackground': 'systembuttonface',
 'highlightcolor': 'systemwindowframe',
 'highlightthickness': ,
 'image': '',
 'justify': 'center',
 'padx': ,
 'pady': ,
 'relief': 'flat',
 'state': 'normal',
 'takefocus': '0',
 'text': 'this is a label',
 'textvariable': '',
 'underline': -1,
 'width': 100,
 'wraplength': }

知道标签的所有属性后,那你就可以通过更改它们来得到不同的标签外观。


更改标签文字字体

你可以用下面的例子来改变标签中的文字字体。

from sys import version_info
if version_info.major == 2:
    import tkinter as tk
elif version_info.major == 3:
    import tkinter as tk
import tkfont    
app = tk.tk()
labelexample1 = tk.label(app, text="customized label 1", font=("times", 20))
labelexample2 = tk.label(app, text="customized label 2", 
                         font=("times", 20, "italic"))
labelfont3 = tkfont.font(family="helvetica", size=20, weight=tkfont.bold,
                         underline=1, overstrike=1)
labelexample3 = tk.label(app, text="customized label 3", 
                         font=labelfont3)
labelexample1.pack()
labelexample2.pack()
labelexample3.pack()
app.mainloop()

tkinter label with modified text font


通过元组来设置标签字体

labelexample1 = tk.label(app, text="customized label 1", font=("times", 20))
labelexample2 = tk.label(app, text="customized label 2", 
                         font=("times", 20, "italic"))

字体元组的第一个元素是字体名字,随后的元素是大小,格式比如加粗,斜体,下划线或者删除线。


用 tkfont 字体对象来设置标签字体

labelfont3 = tkfont.font(family="helvetica", size=20, weight=tkfont.bold,
                         underline=1, overstrike=1)
labelexample3 = tk.label(app, text="customized label 3", 
                         font=labelfont3)

设置标签文本字体属性的另外一种方法是使用 tkfont 模块中的字体对象。labelexample3 的字体类型就是 helvetica 字体系列,20 号,加粗,有下划线以及粗细为 1 的删除线。

备注 也许你想知道 tkinter 中可用的字体系列,那下面的这段小程序就会把它们全部列出来。

from sys import version_info
if version_info.major == 2:
    import tkinter as tk
elif version_info.major == 3:
    import tkinter as tk
import tkfont
from pprint import pprint
app = tk.tk()
pprint(tkfont.families())  

更改标签颜色

fgbg 属性分别确定了标签控件的前景色及背景色。

labelexample1 = tk.label(app, text="customized color",bg="gray", fg="red")

tkinter change widget foreground and background color


标签中显示图片

标签中 image 属性可以用来在标签中显示图片。

from sys import version_info
if version_info.major == 2:
    import tkinter as tk
elif version_info.major == 3:
    import tkinter as tk
    
app = tk.tk()
logo = tk.photoimage(file='python.gif')
labelexample = tk.label(app, image=logo)
labelexample.pack()
app.mainloop()

tkinter image in label

警告 tk.photoimage 只能显示 gif,ppm/ppm/pgm 格式的图片。如果图片是其他格式的话,那它会报如下错误,_tkinter.tclerror: couldn’t recognize data in image file

查看笔记

扫码一下
查看教程更方便
网站地图