KrasnokutskiyEA
KrasnokutskiyEA

Reputation: 597

TypeError: changeText() takes exactly 2 arguments (0 given)

Im using python (2.7.5) and pyqt (4.8.4) and i want to create a simple gui programm that displays value of the function (increment) in a "lineEdit" object everytime the button is clicked. My code:

z=0
def myfunc1():
        global z
        z=z+1
        print (z)


def changeText(self, event):
        lineEdit.setText(str(z))


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(613, 545)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(244, 352, 111, 51))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(242, 290, 111, 20))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), myfunc1)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), changeText)


    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "click me", None))
        self.lineEdit.setText(_translate("Form", "functionvalue", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

Programm begins to work, but after clicking the button it gives an error message: TypeError: changeText() takes exactly 2 arguments (0 given) As far as i understand, i should somehow define those 2 arguments: (self, event)..But how?

Upvotes: 0

Views: 2076

Answers (2)

Frodon
Frodon

Reputation: 3775

Here is a working code:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Dialog"))
        Form.resize(151, 67)
        self.verticalLayout = QtGui.QVBoxLayout(Form)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.verticalLayout.addWidget(self.pushButton)
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setReadOnly(True)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.verticalLayout.addWidget(self.lineEdit)

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.changeText)


    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "Click me", None))
        self.lineEdit.setPlaceholderText(_translate("Form", "functionvalue", None))


class MyForm(QtGui.QDialog, Ui_Form):
    def __init__(self, z=0):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.z = z

    def myfunc1(self):
        self.z+=1
        print (self.z)

    def changeText(self):
        self.myfunc1()
        self.lineEdit.setText(str(self.z))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = MyForm()
    Form.show()
    sys.exit(app.exec_())

Upvotes: 1

Serial
Serial

Reputation: 8043

you gave changetext() the self argument even though it isn't in the class UI_form so it doesnt need the self

then when you called it you didn't give any arguments with it on this line:

QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), changeText)

and its expecting to parameters to be called with it becuase when it was defined you gave it a self and an event argument

Upvotes: 1

Related Questions