Python GUI 编程:tkinter 初学者入门指南——Ttk 选项卡 Notebook
ztj100 2025-06-09 07:27 18 浏览 0 评论
在本文中,将介绍如何使用 Tkinter Notebook 小部件创建选项卡。
Notebook 是由 Tkinter Ttk 模块引入的强大小部件。允许开发者创建包含多个选项卡的界面,每个选项卡可以包含不同的内容。
创建 Notebook 小部件,请使用如下构造函数:
notebook = ttk.Notebook(master,**kw)
添加选项卡
有两种方式可以为 Notebook 小部件添加选项卡。使用 add() 方法,在末尾附加一个新选项卡。使用 insert() 方法,可将选项卡添加到特定位置。
add() 方法
add(child, **kwargs)
insert() 方法
insert(location, child, **kwargs)
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Notebook 选项卡演示')
label = ttk.Label(root, text = "选项卡演示")
label.pack(ipadx=5, ipady=5)
notebook = ttk.Notebook(root)
notebook.pack(fill='both', expand=True)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
label1 = ttk.Label(frame1, text = "第一个选项卡区域")
label1.pack(expand=True)
label2 = ttk.Label(frame2, text = "第二个选项卡区域")
label2.pack(expand=True)
frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)
# 方法1
notebook.add(frame1, text='选项卡1')
notebook.add(frame2, text='选项卡2')
frame3 = ttk.Frame(notebook)
label3 = ttk.Label(frame3, text = "第三个选项卡区域")
label3.pack(expand=True)
frame3.pack(fill= tk.BOTH, expand=True)
# 方法2
notebook.insert("end", frame3, text = "选项卡3")
root.mainloop()
访问选项卡
select()
select() 方法,不带任何参数将返回当前选定的选项卡。使用参数可以切换选项卡。
select(tab_id)
tab() 方法
tab() 方法,可以通过选项卡的 ID 访问选项卡的某些选项。
tab(tab_id, **kwargs)
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Notebook 选项卡演示')
def tab_selected(event):
tab_id = notebook.select()
tab_text = notebook.tab(tab_id, 'text')
label2['text'] = tab_text
frame = ttk.Frame(root)
label1 = ttk.Label(frame, text = "当前选项卡:")
label1.pack(side=tk.LEFT,)
label2 = ttk.Label(frame, text = "")
label2.pack()
frame.pack()
notebook = ttk.Notebook(root)
notebook.pack(fill='both', expand=True)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
label3 = ttk.Label(frame1, text = "第一个选项卡区域")
label3.pack(expand=True)
label4 = ttk.Label(frame2, text = "第二个选项卡区域")
label4.pack(expand=True)
frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)
notebook.add(frame1, text='选项卡1')
notebook.add(frame2, text='选项卡2')
notebook.bind("<<NotebookTabChanged>>", tab_selected)
# 默认选择第二个选项卡
notebook.select(1)
root.mainloop()
自定义选项卡
将图像添加到选项卡标题
使用 Notebook 的 image 选项,将图片添加到选项卡的标题
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Notebook 选项卡演示')
label1 = ttk.Label(root, text = "自定义选项卡")
label1.pack()
notebook = ttk.Notebook(root)
notebook.pack(fill='both', expand=True)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
label3 = ttk.Label(frame1, text = "第一个选项卡区域")
label3.pack(expand=True)
label4 = ttk.Label(frame2, text = "第二个选项卡区域")
label4.pack(expand=True)
frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)
tab_image = tk.PhotoImage(file = "exit.png")
notebook.add(frame1, text='选项卡1', image = tab_image, compound = "left")
notebook.add(frame2, text='选项卡2')
root.mainloop()
垂直方向选项卡
自定义 Ttk Notebook 样式,设置参数 tabposition 为 wn,创建垂直方向选项卡。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Notebook 选项卡演示')
label1 = ttk.Label(root, text = "自定义选项卡")
label1.pack()
style = ttk.Style()
style.configure("Custom.TNotebook", tabposition="wn")
notebook = ttk.Notebook(root, style="Custom.TNotebook")
notebook.pack(fill='both', expand=True)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
label3 = ttk.Label(frame1, text = "第一个选项卡区域")
label3.pack(expand=True)
label4 = ttk.Label(frame2, text = "第二个选项卡区域")
label4.pack(expand=True)
frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)
notebook.add(frame1, text='选项卡1')
notebook.add(frame2, text='选项卡2')
root.mainloop()
更改主题美化选项卡
使用 theme_create() , style.theme_use 自定义更改当前主题,美化选项卡。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Notebook 选项卡演示')
style = ttk.Style()
style.theme_create( "dummy", parent="alt", settings={
"TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } },
"TNotebook.Tab": {"configure": {"padding": [5, 1], "background": "#DCF0F2" },
"map": {"background": [("selected", "#F2C84B")], "expand": [("selected", [1, 1, 1, 0])] } } } )
style.theme_use("dummy")
notebook = ttk.Notebook(root)
notebook.pack(fill='both', expand=True)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
label3 = ttk.Label(frame1, text = "第一个选项卡区域")
label3.pack(expand=True)
label4 = ttk.Label(frame2, text = "第二个选项卡区域")
label4.pack(expand=True)
frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)
notebook.add(frame1, text='选项卡1')
notebook.add(frame2, text='选项卡2')
root.mainloop()
相关推荐
- 这个 JavaScript Api 已被废弃!请慎用!
-
在开发过程中,我们可能会不自觉地使用一些已经被标记为废弃的JavaScriptAPI。这些...
- JavaScript中10个“过时”的API,你的代码里还在用吗?
-
JavaScript作为一门不断发展的语言,其API也在持续进化。新的、更安全、更高效的API不断涌现,而一些旧的API则因为各种原因(如安全问题、性能瓶颈、设计缺陷或有了更好的替代品)被标记为“废...
- 几大开源免费的 JavaScript 富文本编辑器测评
-
MarkDown编辑器用的时间长了,发现发现富文本编辑器用起来是真的舒服。...
- 比较好的网页里面的 html 编辑器 推荐
-
如果您正在寻找嵌入到网页中的HTML编辑器,以便用户可以直接在网页上编辑HTML内容,以下是几个备受推荐的:CKEditor:CKEditor是一个功能强大的、开源的富文本编辑器,可以嵌入到...
- Luckysheet 实现excel多人在线协同编辑
-
前言前些天看到Luckysheet支持协同编辑Excel,正符合我们协同项目的一部分,故而想进一步完善协同文章,但是遇到了一下困难,特此做声明哈,若侵权,请联系我删除文章!若侵犯版权、个人隐私,请联系...
- 从 Element UI 源码的构建流程来看前端 UI 库设计
-
作者:前端森林转发链接:https://mp.weixin.qq.com/s/ziDMLDJcvx07aM6xoEyWHQ引言...
- 手把手教你如何用 Decorator 装饰你的 Typescript?「实践」
-
作者:Nealyang转发连接:https://mp.weixin.qq.com/s/PFgc8xD7gT40-9qXNTpk7A...
- 推荐五个优秀的富文本编辑器
-
富文本编辑器是一种可嵌入浏览器网页中,所见即所得的文本编辑器。对于许多从事前端开发的小伙伴来说并不算陌生,它的应用场景非常广泛,平时发个评论、写篇博客文章等都能见到它的身影。...
- 基于vue + element的后台管理系统解决方案
-
作者:林鑫转发链接:https://github.com/lin-xin前言该方案作为一套多功能的后台框架模板,适用于绝大部分的后台管理系统(WebManagementSystem)开发。基于v...
- 开源富文本编辑器Quill 2.0重磅发布
-
开源富文本编辑器Quill正式发布2.0版本。官方TypeScript声明...
- Python之Web开发框架学习 Django-表单处理
-
在Django中创建表单实际上类似于创建模型。同样,我们只需要从Django类继承,则类属性将是表单字段。让我们在myapp文件夹中添加一个forms.py文件以包含我们的应用程序表单。我们将创建一个...
- Django测试入门:打造坚实代码基础的钥匙
-
这一篇说一下django框架的自动化测试,...
- Django ORM vs SQLAlchemy:到底谁更香?从入门到上头的选择指南
-
阅读文章前辛苦您点下“关注”,方便讨论和分享,为了回馈您的支持,我将每日更新优质内容。...
- 超详细的Django 框架介绍,它来了!
-
时光荏苒,一晃小编的Tornado框架系列也结束了。这个框架虽然没有之前的FastAPI高流量,但是,它也是小编的心血呀。总共16篇博文,从入门到进阶,包含了框架的方方面面。虽然小编有些方面介绍得不是...
- 20《Nginx 入门教程》使用 Nginx 部署 Python 项目
-
今天的目标是完成一个PythonWeb项目的线上部署,我们使用最新的Django项目搭建一个简易的Web工程,然后基于Nginx服务部署该PythonWeb项目。1.前期准备...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)