百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT知识 > 正文

Python GUI 编程:tkinter 初学者入门指南——Ttk 选项卡 Notebook

liuian 2025-06-10 16:27 21 浏览

在本文中,将介绍如何使用 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()

相关推荐

总结下SpringData JPA 的常用语法

SpringDataJPA常用有两种写法,一个是用Jpa自带方法进行CRUD,适合简单查询场景、例如查询全部数据、根据某个字段查询,根据某字段排序等等。另一种是使用注解方式,@Query、@Modi...

解决JPA在多线程中事务无法生效的问题

在使用SpringBoot2.x和JPA的过程中,如果在多线程环境下发现查询方法(如@Query或findAll)以及事务(如@Transactional)无法生效,通常是由于S...

PostgreSQL系列(一):数据类型和基本类型转换

自从厂子里出来后,数据库的主力就从Oracle变成MySQL了。有一说一哈,贵确实是有贵的道理,不是开源能比的。后面的工作里面基本上就是主MySQL,辅MongoDB、ES等NoSQL。最近想写一点跟...

基于MCP实现text2sql

目的:基于MCP实现text2sql能力参考:https://blog.csdn.net/hacker_Lees/article/details/146426392服务端#选用开源的MySQLMCP...

ORACLE 错误代码及解决办法

ORA-00001:违反唯一约束条件(.)错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。ORA-00017:请求会话以设置跟踪事件ORA-00018:超出最大会话数ORA-00...

从 SQLite 到 DuckDB:查询快 5 倍,存储减少 80%

作者丨Trace译者丨明知山策划丨李冬梅Trace从一开始就使用SQLite将所有数据存储在用户设备上。这是一个非常不错的选择——SQLite高度可靠,并且多种编程语言都提供了广泛支持...

010:通过 MCP PostgreSQL 安全访问数据

项目简介提供对PostgreSQL数据库的只读访问功能。该服务器允许大型语言模型(LLMs)检查数据库的模式结构,并执行只读查询操作。核心功能提供对PostgreSQL数据库的只读访问允许L...

发现了一个好用且免费的SQL数据库工具(DBeaver)

缘起最近Ai不是大火么,想着自己也弄一些开源的框架来捣腾一下。手上用着Mac,但Mac都没有显卡的,对于学习Ai训练模型不方便,所以最近新购入了一台4090的拯救者,打算用来好好学习一下Ai(呸,以上...

微软发布.NET 10首个预览版:JIT编译器再进化、跨平台开发更流畅

IT之家2月26日消息,微软.NET团队昨日(2月25日)发布博文,宣布推出.NET10首个预览版更新,重点改进.NETRuntime、SDK、libraries、C#、AS...

数据库管理工具Navicat Premium最新版发布啦

管理多个数据库要么需要使用多个客户端应用程序,要么找到一个可以容纳你使用的所有数据库的应用程序。其中一个工具是NavicatPremium。它不仅支持大多数主要的数据库管理系统(DBMS),而且它...

50+AI新品齐发,微软Build放大招:拥抱Agent胜算几何?

北京时间5月20日凌晨,如果你打开微软Build2025开发者大会的直播,最先吸引你的可能不是一场原本属于AI和开发者的技术盛会,而是开场不久后的尴尬一幕:一边是几位微软员工在台下大...

揭秘:一条SQL语句的执行过程是怎么样的?

数据库系统能够接受SQL语句,并返回数据查询的结果,或者对数据库中的数据进行修改,可以说几乎每个程序员都使用过它。而MySQL又是目前使用最广泛的数据库。所以,解析一下MySQL编译并执行...

各家sql工具,都闹过哪些乐子?

相信这些sql工具,大家都不陌生吧,它们在业内绝对算得上第一梯队的产品了,但是你知道,他们都闹过什么乐子吗?首先登场的是Navicat,这款强大的数据库管理工具,曾经让一位程序员朋友“火”了一把。Na...

详解PG数据库管理工具--pgadmin工具、安装部署及相关功能

概述今天主要介绍一下PG数据库管理工具--pgadmin,一起来看看吧~一、介绍pgAdmin4是一款为PostgreSQL设计的可靠和全面的数据库设计和管理软件,它允许连接到特定的数据库,创建表和...

Enpass for Mac(跨平台密码管理软件)

还在寻找密码管理软件吗?密码管理软件有很多,但是综合素质相当优秀且完全免费的密码管理软件却并不常见,EnpassMac版是一款免费跨平台密码管理软件,可以通过这款软件高效安全的保护密码文件,而且可以...