PyQt5: Creation of Windows with Classes [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



PyQt5: Creation of Windows with Classes [duplicate]



This question already has an answer here:



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.


super().__init__()



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



(Edited to clarify question)



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




1 Answer
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.





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'
– Destroxia
1 hour ago



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





I'd need to know the arguments you're using for init...
– musicamante
2 mins ago

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered