2014-04-19 88 views
2

我创建了一个基于Tkinter的应用程序,它使用Matplotlib绘制波形。我想知道如何改变Matplotlib工具栏按钮的工具提示(因为我的应用程序是捷克语,我需要翻译英文描述)。我还想更改/翻译或仅删除显示在工具栏面板旁边的说明(pan/zoom,zoom rect),方法是单击缩放或平移按钮。Matplotlib/Tkinter - 自定义工具栏工具提示

我发现了一些有关如何添加或删除工具栏按钮的有用技巧,但没有找到任何建议来定制工具提示/说明。我认为这与前一种情况类似,我需要基于NavigationToolbar2TkAgg创建一个新的工具栏类,并以某种方式对其进行修改。任何建议如何做到这一点?提前谢谢了。

回答

6

PART 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 np 
import Tkinter as tk 
import matplotlib as mpl 
from matplotlib.patches import Rectangle 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg 

# custom toolbar with lorem ipsum text 
class 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() 

这应该给你一个正常的嵌入式matplotlib的数字,但是当你将鼠标悬停在按钮,你会得到这样的:

Custom toolbar text example

第2部分

问题的第二部分是ele GANT。 “平移/缩放”和“缩放矩形”文本被硬编码到工具栏的panzoom方法中。实际文本保存在工具栏的self.mode属性中。覆盖所产生内容的最简单方法是为基类panzoom方法创建子类封装器。

这些包装在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方法捕捉和文字中的某一位翻译。

+0

非常感谢你,它的工作原理就是我希望它能够工作的方式:-)。 – JirkaK

+0

'toolbar.update()'做了什么?我使用这个例子来修改导航工具栏的默认行为,除了'update()'似乎没有什么区别外,它很好地工作。 –

+0

它更新NavigationToolbar2TkAgg对象的内部状态,我认为它也等待排队的请求完成。 – ebarr