I am writing a program which is supposed to give me data of a chart when I click certain buttons. I have given the program as far as I am right now. (I still have to connect the last 4 buttons but the first one works.) Now there is another problem: I used to have a chart displayed as I clicked on "One Plot" but since I activated "First Button", the chart does not appear anymore due to the problem mentioned problem. This is the script:
class Main(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
self.fig_dict = {}
self.mplfigs.itemClicked.connect(self.changefig)
self.button1.setText("First Point")
self.button1.clicked.connect(self.onClickButton1)
self.dialogbutton1 = PopUp(self)
fig = Figure()
self.addmpl(fig)
@QtCore.pyqtSlot()
def changefig(self, item):
text = item.text()
self.rmmpl()
self.addmpl(self.fig_dict[str(text)])
def addfig(self, name, fig):
self.fig_dict[name] = fig
self.mplfigs.addItem(name)
def addmpl(self, fig):
self.canvas = FigureCanvas(fig)
self.mplvl.addWidget(self.canvas)
self.canvas.draw()
self.toolbar = NavigationToolbar(self.canvas, self.mplwindow, coordinates=True)
self.mplvl.addWidget(self.toolbar)
def onClickButton1(self):
# When button 1 is clicked, I do the following
print "does nothing now."
self.dialogbutton1.exec_()
def rmmpl(self,):
self.mplvl.removeWidget(self.canvas)
self.canvas.close()
self.mplvl.removeWidget(self.toolbar)
self.toolbar.close()
class PopUp(QtGui.QDialog):
def __init__(self, parent=None):
super(PopUp, self).__init__(parent)
self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.textBrowser = QtGui.QTextBrowser(self)
self.textBrowser.append("x - Coordinate = 0 y - Coordinate = 0.031451")
self.verticalLayout = QtGui.QVBoxLayout(self)
self.verticalLayout.addWidget(self.textBrowser)
self.verticalLayout.addWidget(self.buttonBox)
if __name__ == '__main__':
import sys
from PyQt4 import QtGui
import numpy as np
fig1 = Figure()
ax1f1 = fig1.add_subplot(111)
ax1f1.plot(np.random.rand(5))
app = QtGui.QApplication(sys.argv)
main = Main()
main.addfig('One plot', fig1)
print main.fig_dict
main.show()
sys.exit(app.exec_())
In class Main
, you are calling changefig
function as self.mplfigs.itemClicked.connect(self.changefig)
.
But function definition of changefig
is def changefig(self, item):
which expects two arguments - self
and item
.
When you called changefig - self.mplfigs.itemClicked.connect(self.changefig)
, only self is passed. But item is not passed.
That's why you are getting that error.
It should be self.mplfigs.itemClicked.connect(self.changefig(item))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.