Running python code exported from qt creator and converted to python using pyqt5

459 views Asked by At

I recently encountered this error after attempting to run my python script. In short, I simply don't understand how I've fed three arguments. I've included what I considered to be the most relevant snippets of code. Help is very appreciated. Thank you.

Traceback (most recent call last): File "DESeq_rpkm_gui.py", line 946, in disp = Ui_MainWindow() File "DESeq_rpkm_gui.py", line 21, in init self.setupUi(self,QMainWindow) TypeError: setupUi() takes exactly 2 arguments (3 given)

    class Ui_MainWindow(QMainWindow):
        def __init__(self,parent=None):
            QMainWindow.__init__(self,parent)
            self.setupUi(self,QMainWindow)

        def setupUi(self,MainWindow):

and

            if __name__=='__main__':
                app = QApplication(sys.argv)
                disp = Ui_MainWindow()
                disp.show()
                sys.exit(disp.exec_()) 
1

There are 1 answers

0
three_pineapples On BEST ANSWER

When you call a method as object.method(argument1, ...) then object is implicitly passed to the method as the first argument.

So with your current code, the line self.setupUi(self,QMainWindow) is actually passing three arguments to the method setupUi. They are self, self (again) and QMainWindow.

You might want to read this: What is the purpose of self?

Now, you may have been confused by this because PyQt is a bit unorthodox in how you call/use the setupUi method. The documentation is here for your reference, but in short, they expect you to call self.setupUi(self).

This is so that you can use the setupUi method to create your UI in any widget you pass in (or even more than one!). This is explained in the first example of the documentation:

import sys
from PyQt4.QtGui import QApplication, QDialog
from ui_imagedialog import Ui_ImageDialog

app = QApplication(sys.argv)
window = QDialog()
ui = Ui_ImageDialog()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())

You are using what I consider the more conventional approach, and calling setupUi from within your __init__ method. This is covered in the second example in their documentation, and should be called as I outlined above.