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:
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:
dale lane - customize navigation toolbar
The links only told me how to delete some of them, and one works with wx.
How can I add these buttons in the toolbar, without using QPushbutton
or addWidget
methods?. Hope you can help me
------ EDIT ------
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")
And I instantiated doing:
self.navigation_toolbar = MyToolbar()
, instead of:
self.navigation_toolbar = NavigationToolbar2(self.figure_canvas, self)
But, I am getting this error:
TypeError: __init__() takes at least 3 arguments (1 given)
I´ve tried adding *args
and kwargs
, but I do not know what i am missing here.
Is this the way to add a button to the matplotlib´s toolbar? Hope you can help me.