1

I am trying to make an app using PyQt5 with Qt Designer, and have read a book "Qt5 Python GUI Programming cookbook" by B.M.Harwani. When reading the author's example in Chapter 4, I had some questions in OOP concept.

Let's say we have the following GUI and have converted the demoSimpleInheritance.ui file into Python file, called demoSimpleInheritance.py

enter image description here

file: demoSimpleInheritance.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog): 
        Dialog.setObjectName("Dialog")
        Dialog.resize(487, 368)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(60, 30, 101, 16))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(60, 80, 91, 16))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(60, 130, 91, 16))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(60, 180, 111, 16))
        self.label_4.setObjectName("label_4")
        self.labelResponse = QtWidgets.QLabel(Dialog)
        self.labelResponse.setGeometry(QtCore.QRect(100, 230, 291, 61))
        self.labelResponse.setText("")
        self.labelResponse.setObjectName("labelResponse")
        self.ButtonClickMe = QtWidgets.QPushButton(Dialog)
        self.ButtonClickMe.setGeometry(QtCore.QRect(190, 320, 113, 32))
        self.ButtonClickMe.setObjectName("ButtonClickMe")
        self.lineEditCode = QtWidgets.QLineEdit(Dialog)
        self.lineEditCode.setGeometry(QtCore.QRect(200, 30, 171, 21))
        self.lineEditCode.setObjectName("lineEditCode")
        self.lineEditName = QtWidgets.QLineEdit(Dialog)
        self.lineEditName.setGeometry(QtCore.QRect(200, 80, 171, 21))
        self.lineEditName.setObjectName("lineEditName")
        self.lineEditHistoryMarks = QtWidgets.QLineEdit(Dialog)
        self.lineEditHistoryMarks.setGeometry(QtCore.QRect(200, 130, 171, 21))
        self.lineEditHistoryMarks.setObjectName("lineEditHistoryMarks")
        self.lineEditGeographyMarks = QtWidgets.QLineEdit(Dialog)
        self.lineEditGeographyMarks.setGeometry(QtCore.QRect(200, 180, 171, 21))
        self.lineEditGeographyMarks.setObjectName("lineEditGeographyMarks")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "Student Code"))
        self.label_2.setText(_translate("Dialog", "Student Name"))
        self.label_3.setText(_translate("Dialog", "History Marks"))
        self.label_4.setText(_translate("Dialog", "Geography Marks"))
        self.ButtonClickMe.setText(_translate("Dialog", "Click"))

After this, we make another Python file, called callSimpleInheritances.py for the real implementation, e.g. make the push button responsive

file: callSimpleInheritances.py

import sys
from PyQt5.QtWidgets import QDialog, QApplication
from demoSimpleInheritance import *

class Student:
    name = ''
    code = ''
    def __init__(self, code, name):
        self.code = code
        self.name = name
    def getCode(self):
        return self.code
    def getName(self):
        return self.name

class Marks(Student):
    historyMarks = 0
    geographyMarks = 0
    def __init__(self, code, name, historyMarks, geographyMarks):
        Student.__init__(self, code, name) 
        self.historyMarks = historyMarks
        self.geographyMarks = geographyMarks
    def getHistoryMarks(self):
        return self.historyMarks
    def getGeographyMarks(self):
        return self.geographyMarks

class MyForm(QDialog):
    def __init__(self):  
        super().__init__()  
        self.ui = Ui_Dialog() 
        self.ui.setupUi(self)  
        self.ui.ButtonClickMe.clicked.connect(self.dispmessage)
        self.show()

    def dispmessage(self):
        marksObj = Marks(self.ui.lineEditCode.text(), self.ui.lineEditName.text(),
                         self.ui.lineEditHistoryMarks.text(), self.ui.lineEditGeographyMarks.text())
        self.ui.labelResponse.setText("Code: " + marksObj.getCode() + ", Name: " + marksObj.getName()
                                      + ", History Marks: " + marksObj.getHistoryMarks() + ", Geography Marks: "
                                      + marksObj.getGeographyMarks())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec_())

I have the following questions:

In demoSimpleInheritance.py:

(1) why is the __init__ method missing in Ui_Dialog class?

In callSimpleInheritance.py:

(2) Marks inherits from Student, MyForm inherits from QDialog, but their __init__ methods are different:

Marks class uses Student.__init__(self, code, name), while MyForm class uses super().__init__(), may I ask when to use which?

Also, I tried using super().__init__(self, code, name) in Marks class and it did not work.

(3) I have seen in some examples, the author put arguments in super().__init__(), e.g. super(*args).__init__(*args), may I ask when this is the case?

(4) MyForm class has an attribute called ui, e.g. self.ui = Ui_Dialog(), but MyForm has no other argument in __init__(self), e.g. why did we not use __init__(self, ui)?

(5) setupUi(self, Dialog) has argument Dialog in demoSimpleInheritance.py, but has not seen this argument in callSimpleInheritance.py, e.g. self.ui.setupUi(self). May I ask why?

Please also point me some references if any, thank you.

RyanKao
  • 321
  • 1
  • 5
  • 14
  • You have many different questions so you risk having your question closed by one of them as I have done now, instead if you have n questions then you must create n post. In this case I have closed as a duplicate of question (1) and (2). – eyllanesc May 05 '20 at 20:32
  • About (3) nothing can be said because we do not see any valid related code, only a reference to the code of the book that you do not show so the community does not know it. – eyllanesc May 05 '20 at 20:34
  • question (4) Your question doesn't make sense since the creation of a MyForm attribute has nothing to do with `__init__(self)` – eyllanesc May 05 '20 at 20:37
  • I have also added a duplicate for (5). Note: "self" has a meaning relative to the class, for example in `def setupUi(self, Dialog):` it is the instance Ui_Dialog but in `self.ui.setupUi(self)` it is the instance `MyForm` – eyllanesc May 05 '20 at 20:38
  • what do you mean by "instance T"? what does "T" refer to? – RyanKao May 05 '20 at 20:42
  • All of the above shows that you have to review your notes about OOP, OOP is a requirement for Qt, that is, you do not have to learn because OOP is used in PyQt, but OOP is a basic topic to handle many technologies. And unfortunately SO is not the right place for that since we have little space besides the objective of the site is another. – eyllanesc May 05 '20 at 20:43
  • Opps, I already fixed the comment – eyllanesc May 05 '20 at 20:44
  • Hi I have reviewed your response, but I think they partially answered my questions. I have basic understanding of OOP. For example, in (1), I was asking why ```__init__``` is ignored in this case, but you point me to the basic definition of ```__init__``` method. in (2), I see explanation regarding ```super()```, but there is another case ```Student.__init__(self, code, name)``` which was not answered. in (4), can you explain why did you say "the creation of MyForm attribute has nothing to do with ```__init__(self)```? Thanks. – RyanKao May 05 '20 at 21:06
  • I don't think half an hour is enough to analyze the answers to the duplicate questions. `__init__` is only implemented if you want to initialize arguments and clearly it is not the objective of `Ui_Dialog`, in the case of `Student.__init__(self, code, name)` it is already answered here: https://stackoverflow.com/a/27134600/6622587 – eyllanesc May 05 '20 at 21:11
  • In `self.ui = Ui_Dialog()` an object of type Ui_Dialog is being created and I don't understand your argument that "ui" is a parameter of a constructor, I don't see any logic. – eyllanesc May 05 '20 at 21:13

0 Answers0