Label 标签
Label 是 tkinter 里用来建立文字或图片的标签对象,这篇教学会介绍如何在 tkinter 视窗里加入 Label 标签,并进行像是文字字型、大小、颜色和位置...等参数设定。
快速导览:
因为 Google Colab 不支援 tkinter,所以请使用本机环境 ( 参考:使用 Python 虚拟环境 ) 或使用 Anaconda Jupyter 进行实作 ( 参考:使用 Anaconda )。
加入 Label 标签
建立 tkinter 视窗对象后,透过 Label 方法,就能在视窗对象中建立 label 标签,必要的参数有两个,第一个表示要加入的视窗对象,第二个则是标签参数 ( 通常是文字 text、图片 image ),建立 label 标签后再使用 pack() 方法将其加入 ( 参考 pack 参数设定 ),下方的程序码执行后,会在视窗里加入一段 hello world 的文字 ( 位置、大小和颜色都使用默认值 )。
import tkinter as tk
root = tk.Tk()
root.title('oxxo.studio')
root.geometry('200x200')
mylabel = tk.Label(root, text='hello world') # 建立 label 標籤
mylabel.pack() # 加入視窗中
# tk.Label(root, text='hello world').pack() # 如果不使用變數,也可使用這種做法
root.mainloop()
Label 标签参数设定
加入 Label 之后,可以透过 Label 的参数调整内容的样式,下方列出 Label 和其他元件相同的参数:
| 参数 | 说明 |
|---|---|
| anchor | 摆放位置,可以设定 n、s、w、e、ne、nw、sw、se、center ( e 右,w 左,s 下,n 上 ),默认 center。 |
| text | 文字内容,可以使用换行符 ( \n )。 |
| width | 宽度,单位是字元数,默认 0。 |
| height | 高度,单位是字元数,默认 0。 |
| padx | 内容和标签左右边界的间距 ( px ),默认 1。 |
| pady | 内容和标签上下边界的间距 ( px ),默认 1。 |
| bg/background | 背景颜色,可以使用十六进制色码或颜色名称。 |
| fg/foreground | 文字颜色,可以使用十六进制色码或颜色名称。 |
| font | 字型设定,包含字体、大小 ( px )、粗体 ( bold )、斜体 ( italic )。 |
| justify | 多行文字的对齐方式,可以设定 left、right、center,默认 center。 |
| cursor | 鼠标移动到标签的样式,可以设定 arrow、circle、cross、plus...等,默认 arrow。 |
| relief | 边框样式,可以设定 flat、sunken、raised、groove、ridge、solid,默认 flat。 |
| bd/borderwidth | 边框粗细,默认 1。 |
| textvariable | 文字内容的变数名称,如果变数被修改,文字就会发生变化。 |
| underline | 第几个字元开始加底线 ( 0 为第一个字元, 默认 -1 不加底线 )。 |
| wraplength | 一段文字超过多少宽度 ( px ) 会换行。 |
| image | 图片内容。 |
| bitmap | 使用 bitmap 图示作为标签内容。 |
| compound | 设定文字与图片排列的方式,可以设定 none、center、top、bottom、left、right,默认 none。 |
设定 Label 文字样式
设定 Label 标签的 font 和 fg 参数,就能调整文字的颜色、字体、和字号...等样式。
import tkinter as tk
root = tk.Tk()
root.title('oxxo.studio')
root.geometry('200x200')
mylabel = tk.Label(root,
text='hello world\n大家好',
font=('Arial',20,'bold'),
fg='#f00')
mylabel.pack()
root.mainloop()
加入 bitmap 图示
各个操作系统里,都有一些固定的系统 bitmap 名称可以使用,只要将 Label 里 bitmap 参数设定为对应的名称,就可以显示该 bitmap,下方列出常用的 bitmap 名称:
| 名称 | 说明 |
|---|---|
| error | 错误 |
| hourglass | 沙漏 |
| info | 消息 |
| questhead | 人头里有问号 |
| question | 问号 |
| warning | 惊叹号 |
| gray12 | 浅灰色 |
| gray25 | 浅中灰色 |
| gray50 | 中灰色 |
| gray75 | 身灰色 |
下面的程序码执行后,会在视窗里产生十个 Label,每个 Label 都有一个 bitmap 图示。
import tkinter as tk
root = tk.Tk()
root.title('oxxo.studio')
root.geometry('200x250')
tk.Label(root, bitmap='error').pack()
tk.Label(root, bitmap='hourglass').pack()
tk.Label(root, bitmap='info').pack()
tk.Label(root, bitmap='questhead').pack()
tk.Label(root, bitmap='question').pack()
tk.Label(root, bitmap='warning').pack()
tk.Label(root, bitmap='gray12').pack()
tk.Label(root, bitmap='gray25').pack()
tk.Label(root, bitmap='gray50').pack()
tk.Label(root, bitmap='gray75').pack()
root.mainloop()
如果要同时显示文字与 bitmap,可以额外添加 compound 参数,指定 bitmap 要出现在文字的哪个位置,如果不设定 compound 参数,则不会显示文字,只会显示 bitmap。
import tkinter as tk
root = tk.Tk()
root.title('oxxo.studio')
root.geometry('200x200')
mylabel = tk.Label(root,
text='hello world\n大家好',
font=('Arial',20,'bold'),
fg='#f00',
bitmap='info',
compound='left')
mylabel.pack()
root.mainloop()
设定 Label 样式
下方的程序码设定了 bg、relief、pady 和 padx 参数,就能产生不同样式的 Label 标签。
import tkinter as tk
root = tk.Tk()
root.title('oxxo.studio')
root.geometry('200x200')
mylabel = tk.Label(root,
text='hello world\n大家好',
font=('Arial',20,'bold'),
fg='#fff',
bg='#f50',
relief='groove',
pady=20,
padx=20)
mylabel.pack()
root.mainloop()
使用文字变数
如果要使用 textvariable 参数读取外部变数内容,则需要先利用 StringVar() 建立 tkinter 文字变数,并透过 set 方法提供变数内容,就可以使用 textvariable 参数读取。
import tkinter as tk
root = tk.Tk()
root.title('oxxo.studio')
root.geometry('200x200')
a = tk.StringVar() # 建立文字變數
a.set('hello') # 設定內容
mylabel = tk.Label(root, textvariable=a, font=('Arial',20)) # 內容是 a 變數
mylabe.pack()
root.mainloop()
pack 参数设定
标签建立完成后,会使用 pack() 方法将其添加至视窗中,pack() 方法本身也可以设定一些参数,这些参数可以决定放置的位置和边界,相关参数如下:
| 参数 | 说明 |
|---|---|
| side | Label 靠近视窗的哪边,可以设定 top、left、bottom 和 right,默认 right。 |
| fill | Label 与视窗的大小关系,可以设定 none、x ( 水平填满 )、y ( 垂直填满 ) 和 both ( 水平垂直都填满 ),默认 none。 |
| anchor | Label 在视窗中的锚点位置,可以设定 n、s、w、e、ne、nw、sw、se、center ( e 右,w 左,s 下,n 上 ),默认 center。 |
| padx | Label 与视窗的水平间距。 |
| pady | Label 与视窗的垂直间距。 |
下方的程序码执行后,会将一个标签放在视窗的右上方,并与视窗有着 20 的边界。
import tkinter as tk
root = tk.Tk()
root.title('oxxo.studio')
root.geometry('200x200')
img = tk.PhotoImage(file='test2.png')
mylabel = tk.Label(root,
text='hello world\n大家好',
font=('Arial',20,'bold'),
bg='#ff0')
mylabel.pack(side='right', padx=20, pady=20,anchor='nw')
root.mainloop()
微信扫码关注
抖音扫码关注