我使用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)..但是怎么做?