Adding an item in matplotlib´s toolbar

后端 未结 2 1240
醉梦人生
醉梦人生 2021-01-25 01:21

I created a matplotlib´s figure in a QMainWindow, using PyQt and I am trying to add a button to the matplotlib´s toolbar in my code. This is the NavigationToo

2条回答
  •  滥情空心
    2021-01-25 01:56

    I have solved the problem. I´ ve found this code:

    Matplotlib/Tkinter - customizing toolbar tooltips (broken link)

    So, I created a subclass and added as it says in the link. This is the code:

    class MyToolbar(NavigationToolbar2):
      def __init__(self, figure_canvas, parent= None):
        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'),
            ('Port', 'Select', "select", 'select_tool'),
            )
    
        NavigationToolbar2.__init__(self, figure_canvas, parent= None)
    
      def select_tool(self):
        print "You clicked the selection tool"
    

    And the, you can add this toolbar by writing:

    self.navigation_toolbar = MyToolbar(self.figure_canvas, self)            
    self.navigation_toolbar.update()
    

    If you want to only let your own button, you must erase all the others items from self.toolitems. For example:

    self.toolitems = (
        ('Port', 'Select', "select", 'select_tool'),
        )
    

    With this, you will only see your own button in the NavigationToolbar

    Hope this helps.

提交回复
热议问题