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 获得焦点时高亮颜色。
相关推荐
恋猫de小郭4 分钟前
Google 开源大模型 Gemma4 怎么选,本地跑的话需要什么条件?
前端·人工智能·ai编程
小冷coding4 分钟前
【面试】结合项目整理的场景面试题,覆盖 Java 基础、锁、多线程、数据库、分布式锁 / 事务、消息中间件等核心维度
java·数据库·面试
鬼先生_sir5 分钟前
SpringCloud-GateWay网关
java·spring cloud·gateway
文心快码BaiduComate7 分钟前
Comate搭载GLM-5.1:长程8H,对齐Opus 4.6
前端·后端·架构
橙露8 分钟前
Linux 驱动入门:字符设备驱动框架与编写流程
linux·运维·服务器
熊猫钓鱼>_>13 分钟前
AI驱动的Web应用智能化:WebMCP、WebSkills与WebAgent的融合实践
前端·人工智能·ai·skill·webagent·webmcp·webskills
毛骗导演16 分钟前
OpenClaw Pi Agent 深度解析:嵌入式 Agent 运行时的架构设计与实现
前端·架构
twl17 分钟前
从 RAG 到可持续演化的知识库:llm-wiki 介绍
前端
卓怡学长18 分钟前
m319个人网站的设计与实现
java·数据库·spring·tomcat·maven·intellij-idea