当前位置: 首页 > 面试题库 >

Matplotlib / Tkinter-自定义工具栏工具提示

车子平
2023-03-14
问题内容

我基于Tkinter创建了一个应用程序,该应用程序使用Matplotlib绘制波形。我不知道如何更改Matplotlib工具栏按钮的工具提示(由于我的应用程序是捷克语,因此我需要翻译英文说明)。我还想更改/翻译或仅删除单击缩放或平移按钮时出现在工具栏面板旁边的说明(pan/zoomzoom rect)。

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


问题答案:

第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()

第2部分

问题的第二部分不太优雅。“平移/缩放”和“缩放矩形”的文本被硬编码到工具栏的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方法以捕获和翻译特定的文本位。



 类似资料:
  • 主窗口左上方设有自定义建筑工具栏。在不同编辑窗口中,自定义建筑工具栏所设置的功能项略有不同,详见主窗口说明。 主窗口说明 外部场景编辑窗口是指进入 CampusBuilder 场景编辑器时的主窗口。自定义工具栏设有建筑和恢复所有对象可拾取按钮。 建筑:可选择现有的自定义建筑,并进入建筑内部。 恢复所有对象可拾取:可对设置过拾取方式的模型进行重置,即恢复对象被拾取功能。 建筑内部编辑窗口是指进入自定

  • 底部导航,有图标和文字,响应鼠标事件和当前页面导航 <nav class="bar bar-tab">   <a class="tab-item external active" href="#">     <span class="icon icon-home"></span>     <span class="tab-label">文案</span>   </a>   <a clas

  • 工具栏是位于屏幕底端的固定(在固定和穿透布局类型中)区域,它包含导航元素。 工具栏不包含任何其他部分,内部只含有普通文本。 工具栏布局 工具栏布局非常简单: <div class="toolbar"> <div class="toolbar-inner"> <a href="#" class="link">Link 1</a> <a href="#" cla

  • 工具栏综述 Toolbar basics 在Jquery Mobile中,有两种标准的工具栏:头部栏和尾部栏 头部栏的作用为网站的标题,通常是移动网站页面的第一个元素,一般包括页面的标题文字和最多两个按钮 尾部栏通常是移动网站页面的最后一个元素,在内容和作用上比头部栏更自由一些,但一般也要包含文字和按钮 在头部栏或尾部栏里放置一个水平的导航栏或选项卡栏的做法是很普遍的,所以Jquery Mobil

  • 工具栏在此是指在移动网站和应用中的头部,尾部和内容中的工具条。所以Jquery Mobile提供了一套标准的工具和导航栏的工具,可以在绝大多数情况下直接使用

  • 我有自己的工具栏,我想为我的标题自定义字体,但我无法让它工作。 我想我做对了,但它什么都没做。