python函数x只需要n个参数(给定n-1)

时间:2016-10-12 17:11:03

标签: python arguments

我正在编写一个程序,当我点击某些按钮时,该程序会给我一个图表数据。我现在已经给了这个程序。 (我仍然必须连接最后4个按钮,但第一个按钮可以正常工作。)现在还有另一个问题:我曾经点击过显示的图表" One Plot"但是由于我激活了第一个按钮",由于提到的问题,图表不再出现。这是脚本:

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

1 个答案:

答案 0 :(得分:1)

class Main,您将changefig功能称为self.mplfigs.itemClicked.connect(self.changefig)

changefig的函数定义为def changefig(self, item):,需要两个参数 - selfitem

当你调用changefig - self.mplfigs.itemClicked.connect(self.changefig)时,只传递self。但项目未通过。

这就是你收到错误的原因。

应为self.mplfigs.itemClicked.connect(self.changefig(item))