1

I am kind new on Pyqt5 and trying to make a Import / Export UI. Have couple errors and cant find what Im doing wrong.. I"ll be glad if I can get some Answers..

I guess I am doing some wrong things with init and super but cant figure out.

Here my Code and Error.

from PyQt5 import QtCore, QtGui, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        MainWindow.setWindowTitle("Gen")
        MainWindow.setFixedSize(1250,802)

#Box 1
        self.groupBox = QtWidgets.QGroupBox(MainWindow)
        self.groupBox.setGeometry(5,0, 350,400)
        self.groupBox.setTitle("BOX 1")

#Box 2
        self.groupBox_2 = QtWidgets.QGroupBox(MainWindow)
        self.groupBox_2.setGeometry(5,400,350,400)
        self.groupBox_2.setTitle("BOX 2")

#Import and Export Button at Box 2
        self.But_Import = QtWidgets.QPushButton(self.groupBox_2)
        self.But_Import.setGeometry(5,360,169,30)
        self.But_Import.setText("Import")

        self.But_Export = QtWidgets.QPushButton(self.groupBox_2)
        self.But_Export.setGeometry(176,360,169,30 )
        self.But_Export.setText("Export")

#class ImageLabel(QtWidgets.QLabel):



if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    Window = MainWindow()
    Window.show()
    sys.exit(app.exec_())


and the Error

Traceback (most recent call last):
  File "D:/NAME/Desktop/fiel/Gen_v1.0.0.2.py", line 47, in <module>
    Window = MainWindow()
  File "D:/NAME/Desktop/fiel/Gen_v1.0.0.2.py", line 17, in __init__
    MainWindow.setWindowTitle("Gen")
TypeError: setWindowTitle(self, str): first argument of unbound method must have type 'QWidget'

progmos1
  • 21
  • 3
  • 1
    Don't you need to do `self.setWindowTitle("Gen")` and `self.setFixedSize(1250,802)` instead of `MainWindow.setWindowTitle("Gen")` and `MainWindow.setFixedSize(1250,802)`? – Random Davis Sep 17 '20 at 16:09

1 Answers1

1

I suppose you're inheriting in some way what pyuic generated code does, and I warmly suggest you to avoid to mimic their behavior.

As already suggested in the comments, you should modify the first two lines after the super():

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Gen")
        self.setFixedSize(1250,802)

Why is that?

Both methods are instance methods, and all instance methods need that the first argument is an instance of the class.

Instance methods expect an instance reference as first argument (what is normally called self). When calling the method directly from an instance, the instance argument (self) is implicitly set.

Imagine this:

class MyClass(object):
    def someMethod(self, argument=None):
        print('self is "{}", argument is "{}"'.format(self, argument))

Then run the following:

>>> MyClass.someMethod('hello')
self is "hello", argument is "None"

>>> instance = MyClass()
>>> instance.someMethod('hello')
self is "<__main__.MyClass object at 0xb5c27dec>", argument is "hello"

As you can see, the first attempt shows that self is the string argument you've given, while the second correctly shows that self is an instance of MyClass and the argument is "hello". And that's exactly what has happened in your case: calling the function using the class instead of the instance has made your first string argument the instance argument.

(A very simplicistic) note about method types: PyQt is a binding, and works as a sort of layer between python and the actual Qt C++ objects. In this case, class methods are sometimes "unbound methods" before the class is instanciated while instance methods become "bound methods". The correct explanation is actually a bit more complex, but this is not the place to discuss this.

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • okay.. Now it Worked thanks... I created my GUI with Pyqt designer and changed a bit what i saw from Internet.. do you dont recommendet this? becouse I see very very different ways to build a Programm (GUI) and I dont know which one is the better one.. Can you understand me? and can you suggest me a example which build should I use? – progmos1 Sep 17 '20 at 19:23
  • @musicamente I will be glad :) – progmos1 Sep 17 '20 at 19:23
  • If you saw anybody suggesting to modify the `pyuic` generated files, just ignore them. While theoretically it "works", 99% of the times it just creates errors, unexpected behavior, bugs and misunderstandings. Those files should **never** be modified (unless you *really*, *REALLY* know what you're doing and why). You can study more about this topic by reading the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). – musicamante Sep 17 '20 at 20:16
  • I wont use Designer but I couldnt find a guide how to code self. Becouse everyone coding it differently and I cant know which one is the best way.. I mean for example Some people importing differently (and the codes are than different) or somepeople closes the window differently without if __main__ etc. – progmos1 Sep 17 '20 at 20:21
  • There is no "best" way. There are various (often similar, but sometimes very different) ways and approaches, and they mostly depend on personal code styling habits and program/project specific requirements. If you do some research about PyQt answers here on SO you can try to read and study how users with high reputation deal with UI creation (especially when comparing the code in their answer with that provided in the question). – musicamante Sep 18 '20 at 15:38