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