当前位置: 首页 > news >正文

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获得焦点时高亮颜色。
http://www.lryc.cn/news/461308.html

相关文章:

  • Mac 远程 Windows 等桌面操作系统工具 Microsoft Remote Desktop for Mac 下载安装详细使用教程
  • 初级网络工程师之从入门到入狱(四)
  • MinIO配置与使用
  • 【漏洞复现】SpringBlade menu/list SQL注入漏洞
  • 物联网智能项目(含案例说明)
  • 【YOLOv8改进】 YOLOv8 更换骨干网络之GhostNetV3步骤详解
  • 成绩查询小程序,家长查分超方便~
  • 鸿蒙开发(NEXT/API 12)【上传下载文件】远场通信场景
  • 快速理解AUTOSAR CP的软件架构层次以及各层的作用
  • 【Unity】Unity中接入Admob聚合广告平台,可通过中介接入 AppLovin,Unity Ads,Meta等渠道的广告
  • PythonExcel批量pingIP地址
  • 软媒市场新蓝海:软文媒体自助发布与自助发稿的崛起
  • 【笔记】Day2.5.1查询运费模板列表(未完
  • 阿基米德螺旋线等距取点
  • 2024年全球增强现实(AR)市场分析报告
  • 探索 NetworkX:Python中的网络分析利器
  • Python知识点:基于Python技术,如何使用AirSim进行无人机模拟
  • 《中国林业产业》是什么级别的期刊?是正规期刊吗?能评职称吗?
  • 私域流量下的白酒新传奇:半年破五千万的营销策略揭秘
  • Tomcat 配置:方便运行 Java Web 项目
  • Spring Boot知识管理:机器学习与AI集成
  • Superset SQL模板使用
  • 算法工程师重生之第二十七天(合并区间 单调递增的数字 监控二叉树 总结)
  • 前端开发基础NodeJS+NPM基本使用(零基础入门)
  • 深度学习 nd.random.normal()
  • Redis Geo 数据类型解析:基于 ZSET 的高效地理位置管理0708
  • 爬虫post收尾以及cookie加代理
  • c++STL——map与set的使用及介绍
  • Vxe UI vue vxe-table select 下拉框选项列表数据量超大过大时卡顿解决方法
  • python 基础笔记(其实有点内容的)