0

我使用python(2.7.5)和pyqt(4.8.4),我想创建一个简单的gui程序,每次单击按钮时都会在“lineEdit”对象中显示函数的值(增量)。我的代码:

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

程序开始工作,但是在单击按钮后它会给出一条错误消息: TypeError: changeText() 恰好需要 2 个参数(给定 0) 据我了解,我应该以某种方式定义这 2 个参数:(self,event)..但是怎么做?

4

2 回答 2

1

你给出changetext()了 self 参数,即使它不在课堂上UI_form,所以它不需要 self

那么当你调用它时,你没有在这一行给出任何参数:

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

并且它期望用它调用参数,因为当它被定义时你给了它一个self和一个event参数

于 2013-07-22T17:15:18.940 回答
1

这是一个工作代码:

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_())
于 2013-07-22T23:23:33.630 回答