Python GUI 编程:tkinter 初学者入门指南——框架、标签框架

在本文中,将介绍 tkinter Frame 框架小部件、 LabelFrame 标签框架小部件的使用方法。

Frame 框架

Frame 框架在窗体上建立一个矩形区域,作为一个容器,用于组织分组排列其他小部件。

要创建框架,请使用以下构造函数。

frame = tk.Frame(master, **options)

tkinter 中的每个小部件都需要一个 "parent" 或 "master" 作为第一个参数。当使用框架时,要将框架作为其父级。

复制代码
import tkinter as tk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Frame 框架演示')

leftframe = tk.Frame(root)
leftframe.pack(side=tk.LEFT)

rightframe = tk.Frame(root)
rightframe.pack(side=tk.RIGHT)

button1 = tk.Button(leftframe, text = "Button1")
button1.pack(padx = 10, pady = 10)
button2 = tk.Button(rightframe, text = "Button2")
button2.pack(padx = 10, pady = 10)
button3 = tk.Button(leftframe, text = "Button3")
button3.pack(padx = 10, pady = 10)

root.mainloop()

在上面的代码中,创建了leftframe、 rightframe 两个框架并排左右放置,三个按钮小部件分别放置到不同的框架中。

框架也可以作为分隔线使用。

复制代码
import tkinter as tk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Frame 框架演示')
frame1 = tk.Frame(root, bd=5, relief=tk.RIDGE)
frame1.pack(ipadx = 100)

button1 = tk.Button(frame1, text = "Button1")
button1.pack(padx = 10, pady = 10)
button2 = tk.Button(frame1, text = "Button2")
button2.pack(padx = 10, pady = 10)
button3 = tk.Button(frame1, text = "Button3")
button3.pack(padx = 10, pady = 10)

frame2 = tk.Frame(frame1, bd=5, relief=tk.RIDGE, bg="black")
frame2.pack(fill=tk.X)

button4 = tk.Button(frame1, text = "Button1")
button4.pack(padx = 10, pady = 10)
button5 = tk.Button(frame1, text = "Button2")
button5.pack(padx = 10, pady = 10)
button6 = tk.Button(frame1, text = "Button3")
button6.pack(padx = 10, pady = 10)
root.mainloop()

LabelFrame 标签框架

tkinter 提供了 Frame 小部件的一个变体,称为 LabelFrame。LabelFrame 标签框架除了具备常规框架的功能外,还扩展了一些其他功能。

要创建框架,请使用以下构造函数。

frame = tk.LabelFrame(master, **options)

LabelFrame 标签框架增加了 Text 参数,可以作为框架的标题。默认位置在左上角,也可以使用参数 labelanchor ,改变标题的位置,可选参数如下图所示。

使用 labelwidget 参数,可以把其他小部件放到框架上。

复制代码
import tkinter as tk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('LabelFrame 标签框架演示')

button = tk.Button(root, text = "Hello!")
button.pack(padx = 5, pady = 6)

LabelFrame1 = tk.LabelFrame(root, text='LabelFrame')
LabelFrame1.pack(ipadx = 100)

LabelFrame2 = tk.LabelFrame(root, text='LabelFrame', labelanchor = "se")
LabelFrame2.pack(ipadx = 100)

LabelFrame3 = tk.LabelFrame(root, text='LabelFrame', labelanchor = "s", labelwidget = button)
LabelFrame3.pack(ipadx = 100)

button1 = tk.Button(LabelFrame1, text = "Button1")
button1.pack(padx = 10, pady = 10)
button2 = tk.Button(LabelFrame1, text = "Button2")
button2.pack(padx = 10, pady = 10)
button3 = tk.Button(LabelFrame2, text = "Button3")
button3.pack(padx = 10, pady = 10)

button4 = tk.Button(LabelFrame2, text = "Button4")
button4.pack(padx = 10, pady = 10)
button5 = tk.Button(LabelFrame3, text = "Button5")
button5.pack(padx = 10, pady = 10)
button6 = tk.Button(LabelFrame3, text = "Button6")
button6.pack(padx = 10, pady = 10)
root.mainloop()

Frame LabelFrame 选项

选项名称 说明
bd 边框的宽度。默认 2 像素。
bg 背景颜色。
cursor 鼠标指针类型。
height 框架的高度。
width 框架的宽度。
highlightbackground 背景颜色。
highlightthickness 获得焦点时边框粗细。
relief 框架的边框类型。默认 FLAT。
highlightcolor 获得焦点时高亮颜色。
相关推荐
c***V3231 小时前
Vue优化
前端·javascript·vue.js
q***33373 小时前
oracle 12c查看执行过的sql及当前正在执行的sql
java·sql·oracle
李@十一₂⁰3 小时前
HTML 特殊字体符号
前端·html
qq_401700414 小时前
嵌入式用Unix时间的优势及其C语言转换
服务器·c语言·unix
小奶包他干奶奶5 小时前
Webpack学习——Loader(文件转换器)
前端·学习·webpack
zy happy5 小时前
若依 vue3 报错:找不到模块“@/api/xxxx/xxxxx”或其相应的类型声明。。Vue 3 can not find mod
前端·javascript·vue.js
潘小安6 小时前
Git Worktree + Claude Code:让你的开发效率翻倍的秘密武器
前端
xu_yule6 小时前
Linux_12(进程信号)内核态和用户态+处理信号+不可重入函数+volatile
linux·运维·服务器
meichaoWen6 小时前
【Vue3】vue3的全面学习(一)
前端·javascript·学习
虾..6 小时前
Linux 环境变量&&进程优先级
linux·运维·服务器