import wx import matplotlib from matplotlib.figure import Figure from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar # 设置全局字体配置 matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体,如宋体、黑体等 matplotlib.rcParams['axes.unicode_minus'] = False # 解决负号'-'显示为方块的问题 class MyToolbar(NavigationToolbar): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # 设置按钮的提示语为中文 self.Realize() self.GetToolByPos(0).SetShortHelp('主页') self.GetToolByPos(1).SetShortHelp('后退') self.GetToolByPos(2).SetShortHelp('前进') self.GetToolByPos(4).SetShortHelp('缩放') self.GetToolByPos(8).SetShortHelp('保存') # 更新工具栏显示 self.canvas.Refresh() class MyFrame(wx.Frame): def __init__(self, parent, title): super().__init__(parent, title=title) self.panel = wx.Panel(self) self.figure = Figure() self.canvas = FigureCanvas(self.panel, -1, self.figure) self.toolbar = MyToolbar(self.canvas) self.sizer = wx.BoxSizer(wx.VERTICAL) self.sizer.Add(self.toolbar, 0, wx.EXPAND) self.sizer.Add(self.canvas, 1, wx.EXPAND) self.panel.SetSizer(self.sizer) self.panel.Fit() app = wx.App() frame = MyFrame(None, "统计图") frame.Show() app.MainLoop()
ps:如果不清楚键位的值,可以打印出来看看先
import matplotlib.pyplot as plt from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar fig, ax = plt.subplots() toolbar = NavigationToolbar(fig.canvas, None) #打印各键位说明的元组 print(toolbar.toolitems)
标签:__,工具栏,统计图,self,matplotlib,wxpython,GetToolByPos,import,wx From: https://www.cnblogs.com/babashi9527/p/17548303.html