我試圖對Python中的一個對象(一個繼承自不同類的類的實例 - 具體來說, QtGui.QLabel
),但是在運行期間出現了上述錯誤。對象本身有一些實際內容的字段(通過調試進行驗證),但由於某種原因,我無法「使用」它。錯誤是什麼意思,我該如何解決?對於一些額外的信息,我會說在我嘗試對其執行此操作之前,該對象是從靜態方法返回的。Python:RuntimeError:%S的超級__init __()從來沒有被調用過
子類有它自己的__init__()
功能:
def __init__(self, image, father):
super(AtomicFactory.Image, self).__init__(father)
self.raw_attributes = image.attributes
self.attributes = {}
father.addChild(self)
self.update()
現在,我寫了一個類似的代碼,簡單的一個,即有上線widget.setParent(mw)
同樣的錯誤時,被移動的窗口。
#!/usr/bin/env python
import sys
import copy
from PyQt4 import QtCore, QtGui
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main_widget=QtGui.QWidget()
widget = QtGui.QPushButton('Test')
widget.resize(640, 480)
widget.setParent(main_widget)
widget.move(0, 0)
widget2=QtGui.QPushButton('Test2')
widget2.i=0
widget2.resize(600, 200)
widget2.setParent(main_widget)
widget2.move(640, 0)
def onResize(event):
print event
mw=copy.deepcopy(main_widget)
widget.setParent(mw)
widget2.setParent(mw)
widget.move(0, 0)
widget2.move(640, 480)
main_widget_width=main_widget.width()
widget_width=widget.width()
width2=main_widget_width-widget_width
height2=widget2.height()
widget2.resize(width2, height2)
widget2.move(640, 0)
main_widget.resizeEvent=onResize
def onClick():
size=(widget2.width(), widget2.height())
if(widget2.i%2==0):
widget2.resize(int(size[0]/2), int(size[1]/2))
else:
widget2.resize(size[0]*2, size[1]*2)
widget2.i+=1
QtCore.QObject.connect(widget, QtCore.SIGNAL('clicked()'), onClick)
main_widget.show()
sys.exit(app.exec_())
問題是什麼?
如果你的派生類MyDerived有自己的__init__,嘗試添加'super(MyDerived,self).__ init __()'這一行。 – halex
它的確如此。該對象實際上是之前創建的,並且使用得很好。我也可以在初始階段使用setParent()而沒有問題。然後它被插入到列表中,並且上面描述的靜態方法在列表中「找到」它,然後將其返回給另一個靜態方法。當後者試圖對它使用setParnet()時,返回錯誤。無論如何,子類的__init __()自定義函數是: – Tomer
def __init __(self,image,father): super(AtomicFactory.Image,self).__ init __(father) self.raw_attributes = image.attributes self。屬性= {} father.addChild(self) self.update() – Tomer