【发布时间】:2015-10-08 07:11:34
【问题描述】:
我在 Youtube 教程中发现了这个简单的程序,它使用 QtSide 模块和 python。基本上它所做的是将QLineEdit 连接到QTextBrowser。正如您在下面看到的,整个程序由单个类处理。我有用于多重继承的 super() 函数的基本概念。所以在这里,我不明白super(Form, self).__init__(parent) 语句的作用。在注释了产生以下错误消息的语句后,我尝试运行相同的程序。
错误:
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.
程序代码:
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_()
这里,super()有什么用?
我找到了一个可能与此有关的question。但我不清楚。
【问题讨论】:
标签: python pyside multiple-inheritance super