0

I'm relatively new to Python and am currently working on the QtWidget. In my online course we created our own class that inherits from QtWidgets.QMainWindow. When creating the constructor, we created a parameter called Parent, what do we need it for? I've also read the official documentation, but I can't make any sense of it.

This is the code:

import sys
from qtpy import QtWidgets
from ui.mainwindow import Ui_MainWindow

app = QtWidgets.QApplication(sys.argv)


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

        self.setWindowTitle("Test")

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.button.clicked.connect(self.on_button_click)



    def on_button_click(self):
        self.weight = self.ui.weight.value()
        self.height = self.ui.height.value()

        if self.weight and self.height != 0:
            self.rechnung = (self.weight) / (self.height ** 2)
            self.ui.result.setText(f"Dein BMI hat einen Wert von {round(self.rechnung, 2)}")

        else:
            self.ui.result.setText("Deine eingaben dürfen keine 0 enthalten")



window = MainWindow()

window.show()

sys.exit(app.exec_())

we pass this parameter to the QtWidgets.QMainWindow() class, but for what?

musicamante
  • 41,230
  • 6
  • 33
  • 58
n1klas
  • 1
  • 1
  • 1
    - Your `MainWindow` class of type `QtWidgets.QMainWindow`, it MAY receive an argument at the moment when it is instantiated. . - Since the `parent` argument was set to `None`, gives the possibility to instantiate your MainWindow class WITH or WITHOUT this argument. . - In this example this argument is not being used. But I believe that in the next classes of your course this argument will be further explored. – Jonas Vieira de Souza Jan 27 '23 at 17:12
  • Also, see the documentation about [QObject and object trees](https://doc.qt.io/qt-6/objecttrees.html). – musicamante Jan 27 '23 at 19:22

0 Answers0