我刚刚开始使用PYQT4。我在QTDesingner中生成了文件test.ui并将其转换为test.py:
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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(400, 300)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(280, 30, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(110, 30, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton.setText(_translate("Dialog", "Start", None))
main.py:
from PyQt4 import QtCore, QtGui
import test, sys
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
ui = test.Ui_Dialog
ui.setupUi(window)
QtCore.QObject.connect(ui.pushButton, QtCore.SIGNAL("click()"), lambda: test(ui))
def test(ui):
print(ui.lineEdit.text())
运行main.py后出错:
TypeError:setupUi()只需要2个参数(给定1个)
提前致谢!
答案 0 :(得分:1)
这里的问题是你是静态调用函数而不是传递实例函数需要的'self'参数。
问题在于:
ui = test.Ui_Dialog
ui.setupUi(window)
这样做:
ui = test.Ui_Dialog()
ui.setupUi(window)
将创建一个对象实例并隐式传递'self'变量