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:
我使用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_())
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?
程序开始工作,但是在单击按钮之后,它给出了一个错误消息:TypeError: changeText()只接受2个参数(给定),据我所知,我应该以某种方式定义这两个参数:(self, event)。但如何?
2 个解决方案
#1
1
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_())
#2
1
you gave changetext()
the self argument even though it isn't in the class UI_form
so it doesnt need the self
你给了changetext()自变量,尽管它不在UI_form类中,所以它不需要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
当它被定义为你给它一个自我和一个事件参数时,它期待被调用的参数。
#1
1
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_())
#2
1
you gave changetext()
the self argument even though it isn't in the class UI_form
so it doesnt need the self
你给了changetext()自变量,尽管它不在UI_form类中,所以它不需要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
当它被定义为你给它一个自我和一个事件参数时,它期待被调用的参数。