百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

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

ztj100 2025-06-09 07:27 11 浏览 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()

相关推荐

再说圆的面积-蒙特卡洛(蒙特卡洛方法求圆周率的matlab程序)

在微积分-圆的面积和周长(1)介绍微积分方法求解圆的面积,本文使用蒙特卡洛方法求解圆面积。...

python编程:如何使用python代码绘制出哪些常见的机器学习图像?

专栏推荐...

python创建分类器小结(pytorch分类数据集创建)

简介:分类是指利用数据的特性将其分成若干类型的过程。监督学习分类器就是用带标记的训练数据建立一个模型,然后对未知数据进行分类。...

matplotlib——绘制散点图(matplotlib散点图颜色和图例)

绘制散点图不同条件(维度)之间的内在关联关系观察数据的离散聚合程度...

python实现实时绘制数据(python如何绘制)

方法一importmatplotlib.pyplotaspltimportnumpyasnpimporttimefrommathimport*plt.ion()#...

简单学Python——matplotlib库3——绘制散点图

前面我们学习了用matplotlib绘制折线图,今天我们学习绘制散点图。其实简单的散点图与折线图的语法基本相同,只是作图函数由plot()变成了scatter()。下面就绘制一个散点图:import...

数据分析-相关性分析可视化(相关性分析数据处理)

前面介绍了相关性分析的原理、流程和常用的皮尔逊相关系数和斯皮尔曼相关系数,具体可以参考...

免费Python机器学习课程一:线性回归算法

学习线性回归的概念并从头开始在python中开发完整的线性回归算法最基本的机器学习算法必须是具有单个变量的线性回归算法。如今,可用的高级机器学习算法,库和技术如此之多,以至于线性回归似乎并不重要。但是...

用Python进行机器学习(2)之逻辑回归

前面介绍了线性回归,本次介绍的是逻辑回归。逻辑回归虽然名字里面带有“回归”两个字,但是它是一种分类算法,通常用于解决二分类问题,比如某个邮件是否是广告邮件,比如某个评价是否为正向的评价。逻辑回归也可以...

【Python机器学习系列】拟合和回归傻傻分不清?一文带你彻底搞懂

一、拟合和回归的区别拟合...

推荐2个十分好用的pandas数据探索分析神器

作者:俊欣来源:关于数据分析与可视化...

向量数据库:解锁大模型记忆的关键!选型指南+实战案例全解析

本文较长,建议点赞收藏,以免遗失。更多AI大模型应用开发学习视频及资料,尽在...

用Python进行机器学习(11)-主成分分析PCA

我们在机器学习中有时候需要处理很多个参数,但是这些参数有时候彼此之间是有着各种关系的,这个时候我们就会想:是否可以找到一种方式来降低参数的个数呢?这就是今天我们要介绍的主成分分析,英文是Princip...

神经网络基础深度解析:从感知机到反向传播

本文较长,建议点赞收藏,以免遗失。更多AI大模型应用开发学习视频及资料,尽在...

Python实现基于机器学习的RFM模型

CDA数据分析师出品作者:CDALevelⅠ持证人岗位:数据分析师行业:大数据...

取消回复欢迎 发表评论: