Shay
Shay

Reputation: 43

Type Error: QApplication(List[str]): not enough arguments

Disclaimer: I am relatively new to programming, and especially new to Python. I am trying to learn to build a GUI with PyQt5 and I keep receiving the error "Type Error: QApplication(List[str]): not enough arguments" when trying to launch my application... I don't see any arguments that would make sense to use, and the ones that I have tried(that would be valid) then cause it to say " module.init() takes at most 2 arguments (3 given)"

import sys
from PyQt5 import QtWidgets, QtGui

class Main(QtWidgets.QApplication):
    def __init__(self):
        super(Main, self).__init__()
        self.setGeometry(100, 100, 300, 500)
        self.setWindowTitle('HelloWorld')
        self.setWindowIcon(QtWidget.QIcon('Image.png'))
        self.show()

app = QtWidgets.QApplication(sys.argv)
gui = Main()
sys.exit(app.exec_())

Upvotes: 2

Views: 3526

Answers (2)

eyllanesc
eyllanesc

Reputation: 243937

Viewing your code I noticed that you are confusing QApplication with some Widget.

The QApplication class manages the GUI application's control flow and main settings. It's Not a Widget.

In your case you could use a widget, for example:

import sys
from PyQt5 import QtWidgets, QtGui

class Main(QtWidgets.QWidget):
    def __init__(self):
        super(Main, self).__init__()
        self.setGeometry(100, 100, 300, 500)
        self.setWindowTitle('HelloWorld')
        self.setWindowIcon(QtGui.QIcon('Image.png'))
        self.show()

app = QtWidgets.QApplication(sys.argv)
gui = Main()
sys.exit(app.exec_())

Note: I have changed self.setWindowIcon(QtWidget.QIcon('Image.png')) to self.setWindowIcon(QtGui.QIcon('Image.png'))

Upvotes: 1

zwer
zwer

Reputation: 25789

QtWidgets.QApplication.__init__() requires at the very least a list of arguments to be passed to it when starting. My guess is that your code fails when you try to initialize your own Main class that calls QtWidgets.QApplication init. Try:

class Main(QtWidgets.QApplication):
    def __init__(self, *args, **kwargs):  # allow it to receive any number of arguments
        super(Main, self).__init__(*args, **kwargs)  # forward to 'super' __init__()
        # etc.

# when initializing:
gui = Main(sys.argv)

Upvotes: 1

Related Questions