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 获得焦点时高亮颜色。
相关推荐
风骏时光牛马1 小时前
智能赋能型AI办公一体化系统架构
前端
满栀5851 小时前
vue动态路由效果
前端·javascript·vue.js·前端框架·vue
952361 小时前
Spring-cloud配置中心
java·spring·spring cloud·微服务
土豆~1 小时前
文件名没后缀就打不开:前端文件预览的内容嗅探改造
前端·状态模式
ydd1001001 小时前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
Euato_Key2 小时前
全栈视野学习Cookie——理解 Cookie 的本质
前端
前端开发呀2 小时前
我的 AI 终端配置清单
前端
敲代码的玉米C2 小时前
测试一直在写你的真实数据根
前端·人工智能·架构
真上帝的左手3 小时前
10. 软件设计&架构-Spring Security 7 整合CAS SSO 单点登录
java·spring·架构·sso
执子念的飞鱼3 小时前
File Viewer 进入 2K 节点后,我更在意这 3 条回归
前端·typescript