I'm learning how to develop some simple GUI applications with PyQt5. I'm also learning Python because of it. There are some things about classes that I don't understand.
For example, here I have a simple GUI code:
from PyQt5 import QtWidgets, QtCore
import sys
class Ui_MainWindow:
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setWindowTitle("Simple GUI")
MainWindow.resize(800,600)
self.button = QtWidgets.QPushButton(MainWindow)
self.button.setGeometry(QtCore.QRect(10, 10, 91, 91))
self.button.setObjectName("button1")
self.button.setText("Button")
self.new_dialog = QtWidgets.QDialog()
self.button.clicked.connect(lambda: New_Dialog.setupDialog(self.new_dialog, "Simple Dialog"))
class New_Dialog:
def setupDialog(window, window_name):
window.setObjectName(window_name)
window.setWindowTitle(window_name)
window.resize(600,400)
window.exec()
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The code generates a simple Main window with a button. When I press the button, a new dialog window pops up (depicted in picture).
This is working as I wanted to, but it produces 5 errors:
Method should have "self" as first argument
Instance of 'New_Dialog' has no 'setObjectName' member
Instance of 'New_Dialog' has no 'setWindowTitle' member
Instance of 'New_Dialog' has no 'resize' member
Instance of 'New_Dialog' has no 'exec' member
Analyzing the errors, I wonder: Why I have always to have a self
as an argument in a method of a class if there is no self.variable
inside the method?
When I add self
as argument it show the following error when I press the button:
self.button.clicked.connect(lambda: New_Dialog.setupDialog(self.new_dialog, "Simple Dialog"))
TypeError: setupDialog() missing 1 required positional argument: 'window_name'
What I'm doing wrong?