Python GUI 编程:tkinter 初学者入门指南——Ttk 选项卡 Notebook
itomcoil 2025-05-28 17:52 11 浏览
在本文中,将介绍如何使用 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()
相关推荐
- Python字符串格式化:你真的会用吗?告别混乱代码,看这一篇就够
-
大家好!今天我们来聊聊Python中一个看似简单却暗藏玄机的操作——字符串格式化。你是不是还在用%s拼凑变量?或者写了无数个format()却依然被同事吐槽代码太“复古”?别急,这篇干货带你解锁三种神...
- Python Unicode字符串编程实用教程
-
Unicode是现代文本处理的基础,本教程将介绍Python中的Unicode字符串处理,涵盖从基础概念到高级应用等。一、Unicode基础概念1.1Unicode与编码核心概念:Unicode:字...
- 殊途同归 python 第 6 节:字符串的使用
-
字符串作为Python的基础数据之一,以下是字符串的几种最常用情形,直接上代码1.声明字符串a="helloworld"b='竹杖芒鞋轻胜马,谁怕,一蓑烟雨任平生...
- python爬虫字符串定位开始跟结束(find方法的使用)
-
python爬虫采集的时候会需要对采集的内容进行处理行为,处理什么?简单的说就是处理多余的HTML代码跟确定文章标题跟结尾,还有内容区间,方法如下:首先先是定位,我们先假设我们采集到了一批数据,数据里...
- python 入门到脱坑 基本数据类型—字符串string
-
以下是Python字符串(String)的入门详解,包含基础操作、常用方法和实用技巧,适合初学者快速掌握:一、字符串基础1.定义字符串#单引号/双引号s1='hello's...
- python字符串知识点总结
-
Python字符串知识点总结1.字符串基础字符串是不可变的序列类型可以用单引号(')、双引号(")或三引号('''或""")创建三引号...
- 在 Python 中使用 f-String 格式化字符串
-
在Python3.6中引入的f字符串提供了一种既简洁又可读的字符串格式新方法。f字符串的正式名称为格式化字符串文字,是以f或F为前缀的字符串,其中包含大括号内的表达式。这些表达式在...
- 零起点Python机器学习快速入门-4-3-字符串常用方法
-
Python中字符串的多种操作。包括去除字符串首尾的空格和特定字符、字符串的连接、查找字符在字符串中的位置、字符串之间的比较、计算字符串的长度、大小写转换以及字符串的分割。通过这些操作,我们可以对字...
- Python 中 字符串处理的高效方法,不允许你还不知道
-
以下是Python中字符串处理的高效方法,涵盖常用操作、性能优化技巧和实际应用场景,帮助您写出更简洁、更快速的代码:一、基础高效操作1.字符串拼接:优先用join()代替+原因:join()预...
- Python字符串详解与示例
-
艾瑞巴蒂字符串的干货来了,字符串是程序中最常见的数据类型之一,用来表示数据文本,下面就来介绍下字符串的特性,操作和方法,和一些示例来吧道友:1.字符串的创建在python中字符串可以永单引号(...
- Python中去除字符串末尾换行符的方法
-
技术背景在Python编程中,处理字符串时经常会遇到字符串末尾包含换行符的情况,如从文件中读取每一行内容时,换行符会作为字符串的一部分被读取进来。为了满足后续处理需求,需要将这些换行符去除。实现步骤1...
- 表格编程之争:Python VS VBA?Excel用户:新编程语言才真香!
-
Python和VBA哪个更好用?Python和VBA是两种不同的编程语言,它们都有自己的特点和优缺点。在表格编程方面,VBA在Excel中的应用非常广泛,可以通过宏来实现自动化操作和数据处理,也可以通...
- 用Python把表格做成web可视化图表
-
Python中有一个streamlit库,Streamlit的美妙之处在于您可以直接在Python中创建Web应用程序,而无需了解HTML、CSS或JavaScrip,今天我们就用st...
- 使用 Python 在 PowerPoint 演示文稿中创建或提取表格
-
PowerPoint中的表格是一种以结构化格式组织和呈现数据的方法,类似于Excel或Word等其他应用程序中表格的使用方式。它们提供了一种清晰简洁的方式来显示信息,使您的受众更容易消化和理...
- 用python实现打印表格的方法
-
最近在做表格输出的任务,一般有两种方法实现在控制台打印,一种是根据表格的输出规则自己写代码实现,另外一种是安装python的第三方依赖包prettytable实现这个效果。方法1:根据表格规则写代码...
- 一周热门
- 最近发表
- 标签列表
-
- ps图案在哪里 (33)
- super().__init__ (33)
- python 获取日期 (34)
- 0xa (36)
- super().__init__()详解 (33)
- python安装包在哪里找 (33)
- linux查看python版本信息 (35)
- python怎么改成中文 (35)
- php文件怎么在浏览器运行 (33)
- eval在python中的意思 (33)
- python安装opencv库 (35)
- python div (34)
- sticky css (33)
- python中random.randint()函数 (34)
- python去掉字符串中的指定字符 (33)
- python入门经典100题 (34)
- anaconda安装路径 (34)
- yield和return的区别 (33)
- 1到10的阶乘之和是多少 (35)
- python安装sklearn库 (33)
- dom和bom区别 (33)
- js 替换指定位置的字符 (33)
- python判断元素是否存在 (33)
- sorted key (33)
- shutil.copy() (33)