在 matplotlib 的工具栏中添加一个项目 [英] Adding an item in matplotlib´s toolbar

查看:47
本文介绍了在 matplotlib 的工具栏中添加一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PyQt在 QMainWindow 中创建了matplotlib的图形,我试图在代码中向matplotlib的工具栏添加按钮.这是我创建的 NavigationToolbar:

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 NavigationToolbar that I created:

我使用 addWidget 方法添加了这些按钮.但是,我需要创建一个Icon并将其放在工具栏上.这是我的代码的一部分:

I added those buttons using the addWidget method. But, what I need is to create an Icon and put it on the toolbar. This is some part of my code:

class A(QMainWindow):
  def __init__(self):
    QMainWindow.__init__(self)

    self.mainWidget = QWidget()
    self.setCentralWidget(self.mainWidget)
    layout = QVBoxLayout()
    self.mainWidget.setLayout(layout)

    self.figure_canvas = FigureCanvas(Figure())
    layout.addWidget(self.figure_canvas, 10)

    self.axes = self.figure_canvas.figure.add_subplot(111)

    self.navigation_toolbar = NavigationToolbar2(self.figure_canvas, self)
    self.addToolBar(Qt.TopToolBarArea, self.navigation_toolbar) 

    self.btn_selection_tool3 = QPushButton(, "Connect")
    self.navigation_toolbar.addWidget(self.btn_selection_tool3)

    self.btn_selection_tool2 = QPushButton()
    self.navigation_toolbar.addWidget(self.btn_selection_tool2)

    self.btn_showgrid = QPushButton("Show Grid")
    self.navigation_toolbar.addWidget(self.btn_showgrid)

    self.btn_hidegrid = QPushButton("Hide Grid")
    self.navigation_toolbar.addWidget(self.btn_hidegrid)

app = QApplication(sys.argv)
window = A()
window.show()
sys.exit(app.exec_())

我看到了一些代码和问题,我找到了这些,但我无法完成我需要的.这些是我阅读的链接:

I saw some codes and questions made, and I found these, but I could not accomplish what I need. These are the links that I read:

车道-自定义导航工具栏

修改工具栏

链接只告诉我如何删除其中的一些,其中一个适用于 wx.

The links only told me how to delete some of them, and one works with wx.

如何在工具栏中添加这些按钮,而不使用 QPushbuttonaddWidget 方法?.希望你能帮我

How can I add these buttons in the toolbar, without using QPushbutton or addWidget methods?. Hope you can help me

基于@three_pineapples 的评论,我尝试将这个类添加到我的代码中:

Based on @three_pineapples comment, Ihave tried to add this class to my code:

class MyToolbar(NavigationToolbar2):
  def __init__(self):
    NavigationToolbar2.__init__(self)
    self.iconDir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
        "..", "images", "icons", "")

    self.a = self.addAction(QIcon(iconDir + "BYE2.ico"),
        "Bye", self.bye)
    self.a.setToolTip("GoodBye")

  def bye(self):
    print "See you next time")

我实例化为:

self.navigation_toolbar = MyToolbar(),而不是:

self.navigation_toolbar = NavigationToolbar2(self.figure_canvas,self)

但是,我收到此错误:

TypeError: __init__() 需要至少 3 个参数(给定 1 个)

我已经尝试添加 *argskwargs,但我不知道我在这里遗漏了什么.

I´ve tried adding *args and kwargs, but I do not know what i am missing here.

这是向matplotlib工具栏添加按钮的方法吗?希望您能够帮助我.

Is this the way to add a button to the matplotlib´s toolbar? Hope you can help me.

推荐答案

我已经解决了这个问题.我找到了这个代码:

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

Matplotlib/Tkinter-自定义工具栏工具提示(链接断开)

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

如果你只想让你自己的按钮,你必须从 self.toolitems 中删除所有其他项目.例如:

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'),
    )

这样,您将只能在 NavigationToolbar

希望这会有所帮助.

这篇关于在 matplotlib 的工具栏中添加一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆