python Matplotlib Tkinter-->tab切换3

环境

python:python-3.12.0-amd64

包:

matplotlib 3.8.2
pillow 10.1.0

python 复制代码
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import tkinter as tk
import tkinter.messagebox as messagebox
import tkinter.ttk as ttk

# 创建自定义工具栏类
class MyNavigationToolbar(NavigationToolbar2Tk):
    toolitems = [('Home','回到初始状态','home','home'),
                 ('Back', '后退', 'back', 'back'),
                 ('Home', '前进', 'forward', 'forward'),
                 ('Pan', '平移', 'move', 'pan'),
                 ('Zoom', '缩放', 'zoom_to_rect', 'zoom'),
                 ('Save', '保存', 'filesave', 'save_figure')]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
    def __init__(self, canvas_, window_):
        NavigationToolbar2Tk.__init__(self, canvas_, window_)
        self.custom_button_img1 = tk.PhotoImage(file='figure_pic1.png')  # 创建第一个图片按钮
        self.custom_button = ttk.Button(self, image=self.custom_button_img1, command=lambda: self.toggle_figure2())
        self.custom_button.pack(side=tk.LEFT)  # 添加按钮到工具栏上
        self.custom_button_img2 = tk.PhotoImage(file='figure_pic2.png')  # 创建第二个图片按钮
        self.custom_button2 = ttk.Button(self, image=self.custom_button_img2, command=lambda: print('111.'))
        self.custom_button2.pack(side=tk.LEFT)  # 添加按钮到工具栏上
        self.fig2_visible = False  # 记录figure2的可见性

    def toggle_figure2(self):
        if self.fig2_visible:
            canvas2.get_tk_widget().pack_forget()  # 隐藏figure2
            self.fig2_visible = False
        else:
            canvas2.get_tk_widget().pack()  # 显示figure2
            self.fig2_visible = True


# 创建 Tkinter 窗口
window = tk.Tk()
window.title("Matplotlib in Tkinter")

# 设置窗口大小和位置
window.geometry('800x600')

# 创建选项卡控件
notebook = ttk.Notebook(window)

# 创建第一个选项卡
tab1 = ttk.Frame(notebook)
fig1 = plt.figure()
plt.plot([1, 2, 3], [4, 5, 6])
canvas1 = FigureCanvasTkAgg(fig1, master=tab1)
canvas1.draw()
toolbar1 = MyNavigationToolbar(canvas1, tab1)
toolbar1.update()
toolbar1.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
notebook.add(tab1, text='图表1')

# 创建第二个选项卡
tab2 = ttk.Frame(notebook)
fig2 = plt.figure()
plt.plot([3, 2, 1], [6, 5, 4])
canvas2 = FigureCanvasTkAgg(fig2, master=tab2)
canvas2.draw()
toolbar2 = MyNavigationToolbar(canvas2, tab2)
toolbar2.update()
toolbar2.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
canvas2.get_tk_widget().pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
notebook.add(tab2, text='图表2')

notebook.pack(fill=tk.BOTH, expand=True)

window.mainloop()

图片资源下载(分享-->python Matplotlib Tkinter图片):

链接:https://pan.baidu.com/s/1vFOU52gG0bgK8RYuj-dzOg

提取码:2oy0

相关推荐
zhaoyong22232 分钟前
MySQL 存储过程中字符集与排序规则不匹配导致查询性能下降的解决方案
jvm·数据库·python
sinat_3834373633 分钟前
golang如何从Python转型Go开发_golang从Python转型Go开发攻略
jvm·数据库·python
rockey62742 分钟前
基于AScript的python3脚本语言发布啦!
python·c#·.net·script·python3·eval·expression·function·动态脚本
gqk011 小时前
Python入门
python
Muyuan19981 小时前
28.Paper RAG Agent 开发记录:修复 LLM Rerank 的解析、Fallback 与可验证性
linux·人工智能·windows·python·django·fastapi
代码小书生2 小时前
statistics,一个统计的 Python 库!
开发语言·python
STLearner2 小时前
SIGIR 2026 | LLM × Graph论文总结(图增强LLM,GraphRAG,Agent,多模态,知识图谱,搜索,推
人工智能·python·深度学习·神经网络·机器学习·数据挖掘·知识图谱
FreakStudio2 小时前
MicroPython 内核开发者直接狂喜!这个 Claude 插件市场,把开发全流程做成了「对话式外挂」
python·单片机·嵌入式·面向对象·并行计算·电子diy
老陈说编程3 小时前
12. LangChain 6大核心调用方法:invoke/stream/batch同步异步全解析,新手也能轻松学会
开发语言·人工智能·python·深度学习·机器学习·ai·langchain
给自己做减法3 小时前
rag混合检索
人工智能·python·rag