python Matplotlib Tkinter-->tab切换2

环境

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.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)
        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')

# 创建一个ttk.Notebook控件
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()

# 将画布放置在选项卡中
canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True)

# 创建一个新的框架
frame1 = ttk.Frame(window)
frame1.place(relx=0, rely=0, anchor=tk.NW, relwidth=1, relheight=0.1)

# 将工具栏放置在新框架中
toolbar1 = MyNavigationToolbar(canvas1, frame1)
toolbar1.update()
toolbar1.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5) # 设置高度

# 创建第二个选项卡
tab2 = ttk.Frame(notebook)
fig2 = plt.figure()
plt.plot([3, 2, 1], [6, 5, 4])
canvas2 = FigureCanvasTkAgg(fig2, master=tab2)
canvas2.draw()

# 将画布放置在选项卡中
canvas2.get_tk_widget().pack(fill=tk.BOTH, expand=True)

# 创建一个新的框架
frame2 = ttk.Frame(window)
frame2.place(relx=0, rely=0, anchor=tk.NW, relwidth=1, relheight=0.1)

# 将工具栏放置在新框架中
toolbar2 = MyNavigationToolbar(canvas2, frame2)
toolbar2.update()
toolbar2.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5) # 设置高度

# 将两个选项卡添加到notebook中
notebook.add(tab1, text='图表1')
notebook.add(tab2, text='图表2')

# 将notebook控件放置在窗口的顶部
notebook.place(relx=0, rely=0.1, relwidth=1, relheight=0.9)

# 运行主循环
window.mainloop()

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

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

提取码:2oy0

相关推荐
费弗里3 小时前
Python全栈应用开发利器Dash 3.x新版本介绍(1)
python·dash
李少兄9 天前
解决OSS存储桶未创建导致的XML错误
xml·开发语言·python
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
Vertira9 天前
PyTorch中的permute, transpose, view, reshape和flatten函数详解(已解决)
人工智能·pytorch·python
学Linux的语莫9 天前
python基础语法
开发语言·python
匿名的魔术师9 天前
实验问题记录:PyTorch Tensor 也会出现 a = b 赋值后,修改 a 会影响 b 的情况
人工智能·pytorch·python
Ven%9 天前
PyTorch 张量(Tensors)全面指南:从基础到实战
人工智能·pytorch·python
mahuifa9 天前
PySide环境配置及工具使用
python·qt·环境配置·开发经验·pyside
大熊猫侯佩9 天前
ruby、Python 以及 Swift 语言关于 “Finally” 实现的趣谈
python·ruby·swift
19899 天前
【Dify精讲】第19章:开源贡献指南
运维·人工智能·python·架构·flask·开源·devops