1

I have a PyQt5 application built that looks like so (I know I have a lot of imports, I am learning so I want complete freedom at the moment):

import sys
from PyQt5.QtGui import *
from PyQt5.QWidgets import *
from PyQt5.QtCore import *

class Menu(QMainWindow):

    def __init__(self)
        super().__init__()

        #create bar
        bar = self.menuBar()

        #create bar menus
        file = bar.addMenu("File")
        about = bar.addMenu("About")

        #create actions
        quit_action = QAction("&Quit", self)
        quit_action.setShortcut('Ctrl+Q')
        about_action = QAction("&About...", self)

        #add actions
        file.addAction(quit_action)
        about.addAction(about_action)

        #what to do with actions
        quit_action.triggered.connect(self.quit_func)
        about_action.triggered.connect(self.about_func)

        #window properties
        self.setWindowTitle("Hello World")
        self.resize(600, 400)

        self.show()

    def quit_func(self):
        sys.exit()

    def about_func(self):
        pass

class About(QWidget):

    def __init__(self):
        super().__init__(parent)

        #widgets
        self.l1 = QLabel('Hello World')
        self.l1.setAlignment(Qt.AlignCenter)
        self.l2 = QLabel('Description of the Application')
        self.l2.setAlignment(Qt.AlignCenter)

        #horiz box
        h_box = QHBoxLayout()
        h_box.addStretch()
        h_box.addWidget(self.l2)
        h_box.addStretch()

        #vert box
        v_box = QVBoxLayout()
        v_box.addWidget(self.l1)
        v_box.addLayout(h_box)
        v_box.addStretch()

        self.setLayout(v_box)

        #window properties
        self.setWindowTitle("About Hello World")
        self.setFixedSize(250,150)

        self.show()

if not QApplication.instance()
    app = QApplication(sys.argv)
else:
    app = QApplication.instance()

main = Menu()
main.show()

sys.exit(app.exec())

I want the about_func() function to call to the About() class, so I can open a window separate from my Main Window created by Menu() class.

This code is throwing the error:

TypeError: QMainWindow(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'sip.wrappertype'

in reference to the super().__init__() in line 9.

How could I implement this in a working fashion? Feel free to criticize any aspect of my code.

(Edited to clarify question)

SignalProcessed
  • 371
  • 4
  • 16
  • your current code has several typos: change `def __init__(self)` to `def __init__(self):`, `if not QApplication.instance()` to `if not QApplication.instance():` and `from PyQt5.QWidgets import *` to `from PyQt5.QtWidgets import *` and change `def __init__(self):` to `def __init__(self, parent=None):` in About class, In the latter you use parent in `super().__init__(parent)` but you have never declared it.. After those changes your code works correctly, I do not know where you get the error you point out. – eyllanesc Sep 18 '18 at 19:35

1 Answers1

1

From your code it's not very clear if you are using Python 2 or 3, anyway, the basic syntax of super is:

super(yourClass, instance).method(args)

So, in your case they are both wrong :-) The first one should be:

class Menu(QMainWindow):
    def __init__(self, parent=None):
        super(Menu, self).__init__(parent)

Also, from Python3 the arguments of super() can be omitted, so the second example could be:

class About(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

Read carefully the Built-in Functions. I know it's a long page, but it contains some of the fundamentals of Python, and studying/understanding them is almost mandatory.

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Thank. I implemented this into my code (Python 3.5, btw), and I am receiving another type error. `TypeError: QMainWindow(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'sip.wrappertype'` – SignalProcessed Sep 18 '18 at 00:51
  • I'd need to know the arguments you're using for __init__... – musicamante Sep 18 '18 at 01:51