第1部分
因此,这应该很简单。在
NavigationToolbar2TkAgg从类继承
NavigationToolbar2它可以发现
matplotlib.backend_bases。如果您看一下
NavigationToolbar2TkAgg,将会看到按钮的弹出文本存储在名为的属性中
self.toolitems。此属性是从基类继承的,该基类定义为:
# list of toolitems to add to the toolbar, format is:# ( # text, # the text of the button (often not visible to users) # tooltip_text, # the tooltip shown on hover (where possible) # image_file, # name of the image for the button (without the extension) # name_of_method, # name of the method in NavigationToolbar2 to call # ) toolitems = ( ('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous view', 'back', 'back'), ('Forward', 'Forward to next view', 'forward', 'forward'), (None, None, None, None), ('Pan', 'Pan axes with left mouse, zoom with right', 'move', 'pan'), ('Zoom', 'Zoom to rectangle', 'zoom_to_rect', 'zoom'), (None, None, None, None), ('Subplots', 'Configure subplots', 'subplots', 'configure_subplots'), ('Save', 'Save the figure', 'filesave', 'save_figure'), )每个元组的第二项是鼠标悬停在按钮上时弹出的文本。要覆盖此内容,只需将其子类化并制作自己的版本即可
toolitems。
例如(带有填充文本):
import numpy as npimport Tkinter as tkimport matplotlib as mplfrom matplotlib.patches import Rectanglefrom matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg# custom toolbar with lorem ipsum textclass CustomToolbar(NavigationToolbar2TkAgg): def __init__(self,canvas_,parent_): self.toolitems = ( ('Home', 'Lorem ipsum dolor sit amet', 'home', 'home'), ('Back', 'consectetuer adipiscing elit', 'back', 'back'), ('Forward', 'sed diam nonummy nibh euismod', 'forward', 'forward'), (None, None, None, None), ('Pan', 'tincidunt ut laoreet', 'move', 'pan'), ('Zoom', 'dolore magna aliquam', 'zoom_to_rect', 'zoom'), (None, None, None, None), ('Subplots', 'putamus parum claram', 'subplots', 'configure_subplots'), ('Save', 'sollemnes in futurum', 'filesave', 'save_figure'), ) NavigationToolbar2TkAgg.__init__(self,canvas_,parent_)class MyApp(object): def __init__(self,root): self.root = root self._init_app() # here we embed the a figure in the Tk GUI def _init_app(self): self.figure = mpl.figure.Figure() self.ax = self.figure.add_subplot(111) self.canvas = FigureCanvasTkAgg(self.figure,self.root) self.toolbar = CustomToolbar(self.canvas,self.root) self.toolbar.update() self.plot_widget = self.canvas.get_tk_widget() self.plot_widget.pack(side=tk.TOP, fill=tk.BOTH, expand=1) self.toolbar.pack(side=tk.TOP, fill=tk.BOTH, expand=1) self.canvas.show() # plot something random def plot(self): self.ax.imshow(np.random.normal(0.,1.,size=[100,100]),cmap="hot",aspect="auto") self.figure.canvas.draw()def main(): root = tk.Tk() app = MyApp(root) app.plot() root.mainloop()if __name__ == "__main__": main()第2部分
问题的第二部分不太优雅。“平移/缩放”和“缩放矩形”的文本被硬编码到工具栏的
pan和
zoom方法中。实际文本保存在
self.mode工具栏的属性中。覆盖产生的结果的最简单方法是为基类
pan和
zoom方法创建子类包装。
这些包装器
CustomToolbar从上面进入类,例如:
def pan(self): NavigationToolbar2TkAgg.pan(self) self.mode = "I'm panning!" #<--- whatever you want to replace "pan/zoom" goes here self.set_message(self.mode)def zoom(self): NavigationToolbar2TkAgg.zoom(self) self.mode = "I'm zooming!" #<--- whatever you want to replace "zoom rect" goes here self.set_message(self.mode)
这只是实现此目的的一种方法,另一种可能是包装该
set_message方法以捕获和翻译特定的文本位。



