0
#!/usr/bin/python
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__() # what the meaning of this line
self.initUI()
def initUI(self):
lbl1 = QtGui.QLabel('ZetCode', self)
lbl1.move(15, 10)
lbl2 = QtGui.QLabel('tutorials', self)
lbl2.move(35, 40)
lbl3 = QtGui.QLabel('for programmers', self)
lbl3.move(55, 70)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Absolute')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
或者在'__init__'內有''super''和其他幾十個其他問題已經存在,請在詢問之前做一些研究。 – jonrsharpe
所有你真正需要知道的是,在你的例子中,它或多或少等同於'QWidget .__ init __(self)'(你可以很容易地爲自己測試)。當創建一個子類的實例時,python會自動調用基類的'__init__' - 除非你已經覆蓋它,就像你的例子。通常,如果在子類中重寫'__init__',則還需要顯式調用基類'__init__'。還有其他原因需要專門使用'super'來完成,但嚴格來說,它們並不適用於PyQt,因爲它大多使用單一繼承。 – ekhumoro