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

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

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

相关推荐

Linux集群自动化监控系统Zabbix集群搭建到实战

自动化监控系统...

systemd是什么如何使用_systemd/system

systemd是什么如何使用简介Systemd是一个在现代Linux发行版中广泛使用的系统和服务管理器。它负责启动系统并管理系统中运行的服务和进程。使用管理服务systemd可以用来启动、停止、...

Linux服务器日常巡检脚本分享_linux服务器监控脚本

Linux系统日常巡检脚本,巡检内容包含了,磁盘,...

7,MySQL管理员用户管理_mysql 管理员用户

一、首次设置密码1.初始化时设置(推荐)mysqld--initialize--user=mysql--datadir=/data/3306/data--basedir=/usr/local...

Python数据库编程教程:第 1 章 数据库基础与 Python 连接入门

1.1数据库的核心概念在开始Python数据库编程之前,我们需要先理解几个核心概念。数据库(Database)是按照数据结构来组织、存储和管理数据的仓库,它就像一个电子化的文件柜,能让我们高效...

Linux自定义开机自启动服务脚本_linux添加开机自启动脚本

设置WGCloud开机自动启动服务init.d目录下新建脚本在/etc/rc.d/init.d新建启动脚本wgcloudstart.sh,内容如下...

linux系统启动流程和服务管理,带你进去系统的世界

Linux启动流程Rhel6启动过程:开机自检bios-->MBR引导-->GRUB菜单-->加载内核-->init进程初始化Rhel7启动过程:开机自检BIOS-->M...

CentOS7系统如何修改主机名_centos更改主机名称

请关注本头条号,每天坚持更新原创干货技术文章。如需学习视频,请在微信搜索公众号“智传网优”直接开始自助视频学习1.前言本文将讲解CentOS7系统如何修改主机名。...

前端工程师需要熟悉的Linux服务器(SSH 终端操作)指令

在Linux服务器管理中,SSH(SecureShell)是远程操作的核心工具。以下是SSH终端操作的常用命令和技巧,涵盖连接、文件操作、系统管理等场景:一、SSH连接服务器1.基本连接...

Linux开机自启服务完全指南:3步搞定系统服务管理器配置

为什么需要配置开机自启?想象一下:电商服务器重启后,MySQL和Nginx没自动启动,整个网站瘫痪!这就是为什么开机自启是Linux运维的必备技能。自启服务能确保核心程序在系统启动时自动运行,避免人工...

Kubernetes 高可用(HA)集群部署指南

Kubernetes高可用(HA)集群部署指南本指南涵盖从概念理解、架构选择,到kubeadm高可用部署、生产优化、监控备份和运维的全流程,适用于希望搭建稳定、生产级Kubernetes集群...

Linux项目开发,你必须了解Systemd服务!

1.Systemd简介...

Linux系统systemd服务管理工具使用技巧

简介:在Linux系统里,systemd就像是所有进程的“源头”,它可是系统中PID值为1的进程哟。systemd其实是一堆工具的组合,它的作用可不止是启动操作系统这么简单,像后台服务...

Red Hat Enterprise Linux 10 安装 Kubernetes (K8s) 集群及高级管理

一、前言...

Linux下NetworkManager和network的和平共处

简介我们在使用CentoOS系统时偶尔会遇到配置都正确但network启动不了的问题,这问题经常是由NetworkManager引起的,关闭NetworkManage并取消开机启动network就能正...

取消回复欢迎 发表评论: