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

相关推荐
一晌小贪欢25 分钟前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
小文数模25 分钟前
2026年美赛数学建模C题完整参考论文(含模型和代码)
python·数学建模·matlab
Halo_tjn33 分钟前
基于封装的专项 知识点
java·前端·python·算法
Hgfdsaqwr1 小时前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
weixin_395448911 小时前
export_onnx.py_0130
pytorch·python·深度学习
s1hiyu1 小时前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
2301_763472461 小时前
使用Seaborn绘制统计图形:更美更简单
jvm·数据库·python
无垠的广袤1 小时前
【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测
linux·人工智能·python·opencv·开发板
Duang007_1 小时前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
浒畔居2 小时前
机器学习模型部署:将模型转化为Web API
jvm·数据库·python