I found this simple program in a Youtube tutorial which is used QtSide modules with python. Basically what it does is connect QLineEdit
to a QTextBrowser
. As you can see below, the entire program handled by single class. I have basic idea of super() function which is used in multiple inheritance. So here, I don't understand what super(Form, self).__init__(parent)
statement does. I tried running the same program after commenting that statement which produced below error message.
Error:
Traceback (most recent call last):
File "/home/dazz/Projects/PycharmProjects/FirstTutorial/a2_gui.py", line 35, in <module>
form = Form()
File "/home/dazz/Projects/PycharmProjects/FirstTutorial/a2_gui.py", line 17, in __init__
self.setLayout(layout)
RuntimeError: '__init__' method of object's base class (Form) not called.
Program code:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.browser = QTextBrowser()
self.lineEdit = QLineEdit()
layout = QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.lineEdit)
self.setLayout(layout)
self.lineEdit.returnPressed.connect(self.update_ui)
self.setWindowTitle('Calculate')
def update_ui(self):
try:
text = self.lineEdit.text()
self.browser.append('%s = %s' % (text, eval(text)))
self.lineEdit.selectAll()
except:
self.browser.append('%s is invalid!' % text)
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
Here, what is the use of super()
?
I found a question which may related to this. But it's not clear to me.