0

I'm just starting to work with PYQT4. I generated the file test.ui in QTDesingner and translated it into 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())

After running the main.py I got an error:

TypeError: setupUi() takes exactly 2 arguments (1 given)

Thanks in advance!

gdmkr
  • 3
  • 2

1 Answers1

1

The problem here is that you are calling the function statically and not passing it the 'self' argument that the instance function needs.

The problem is here:

ui = test.Ui_Dialog
ui.setupUi(window)

Doing This:

ui = test.Ui_Dialog()
ui.setupUi(window)

will create an object instance and pass the 'self' variable implicitly

zawata
  • 172
  • 6