• python Matplotlib Tkinter-->tab切换3


    环境
    python:python-3.12.0-amd64
    包:
    matplotlib 3.8.2
    pillow  10.1.0
    1. import matplotlib.pyplot as plt
    2. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
    3. import tkinter as tk
    4. import tkinter.messagebox as messagebox
    5. import tkinter.ttk as ttk
    6. # 创建自定义工具栏类
    7. class MyNavigationToolbar(NavigationToolbar2Tk):
    8. toolitems = [('Home','回到初始状态','home','home'),
    9. ('Back', '后退', 'back', 'back'),
    10. ('Home', '前进', 'forward', 'forward'),
    11. ('Pan', '平移', 'move', 'pan'),
    12. ('Zoom', '缩放', 'zoom_to_rect', 'zoom'),
    13. ('Save', '保存', 'filesave', 'save_figure')]
    14. def __init__(self, *args, **kwargs):
    15. super().__init__(*args, **kwargs)
    16. def __init__(self, canvas_, window_):
    17. NavigationToolbar2Tk.__init__(self, canvas_, window_)
    18. self.custom_button_img1 = tk.PhotoImage(file='figure_pic1.png') # 创建第一个图片按钮
    19. self.custom_button = ttk.Button(self, image=self.custom_button_img1, command=lambda: self.toggle_figure2())
    20. self.custom_button.pack(side=tk.LEFT) # 添加按钮到工具栏上
    21. self.custom_button_img2 = tk.PhotoImage(file='figure_pic2.png') # 创建第二个图片按钮
    22. self.custom_button2 = ttk.Button(self, image=self.custom_button_img2, command=lambda: print('111.'))
    23. self.custom_button2.pack(side=tk.LEFT) # 添加按钮到工具栏上
    24. self.fig2_visible = False # 记录figure2的可见性
    25. def toggle_figure2(self):
    26. if self.fig2_visible:
    27. canvas2.get_tk_widget().pack_forget() # 隐藏figure2
    28. self.fig2_visible = False
    29. else:
    30. canvas2.get_tk_widget().pack() # 显示figure2
    31. self.fig2_visible = True
    32. # 创建 Tkinter 窗口
    33. window = tk.Tk()
    34. window.title("Matplotlib in Tkinter")
    35. # 设置窗口大小和位置
    36. window.geometry('800x600')
    37. # 创建选项卡控件
    38. notebook = ttk.Notebook(window)
    39. # 创建第一个选项卡
    40. tab1 = ttk.Frame(notebook)
    41. fig1 = plt.figure()
    42. plt.plot([1, 2, 3], [4, 5, 6])
    43. canvas1 = FigureCanvasTkAgg(fig1, master=tab1)
    44. canvas1.draw()
    45. toolbar1 = MyNavigationToolbar(canvas1, tab1)
    46. toolbar1.update()
    47. toolbar1.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
    48. canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
    49. notebook.add(tab1, text='图表1')
    50. # 创建第二个选项卡
    51. tab2 = ttk.Frame(notebook)
    52. fig2 = plt.figure()
    53. plt.plot([3, 2, 1], [6, 5, 4])
    54. canvas2 = FigureCanvasTkAgg(fig2, master=tab2)
    55. canvas2.draw()
    56. toolbar2 = MyNavigationToolbar(canvas2, tab2)
    57. toolbar2.update()
    58. toolbar2.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
    59. canvas2.get_tk_widget().pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
    60. notebook.add(tab2, text='图表2')
    61. notebook.pack(fill=tk.BOTH, expand=True)
    62. window.mainloop()

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

    链接:https://pan.baidu.com/s/1vFOU52gG0bgK8RYuj-dzOg 
    提取码:2oy0 

  • 相关阅读:
    kylin系统gcc报错fatal error: stdio.h: 没有那个文件或目录
    C语言数组清零----使用memset函数
    Python自动化运维实战——Telnetlib和Netmiko自动化管理网络设备
    Java面试进阶指北
    基于springboot小区物业管理系统
    极速Go语言入门(超全超详细)-基础篇
    二、BurpSuite Proxy代理
    洛谷P2196 [NOIP1996 提高组] 挖地雷【动态规划思路分析】看完直接举一反三!
    The IDE is running low on memory and this might affect performance.
    # 数据结构
  • 原文地址:https://blog.csdn.net/caoyanchao1/article/details/136293176