【发布时间】:2012-08-30 02:38:20
【问题描述】:
我尝试对 Python 中的一个对象(继承自不同类的类的实例 - 具体而言,QtGui.QLabel)执行一些操作 (setParent),但在运行时引发了上述错误。该对象本身有一些具有实际内容的字段(在调试时验证),但由于某种原因我不能“使用”它。错误是什么意思,我该如何解决?对于一些附加信息,我会说该对象是在我尝试对其执行此操作之前从静态方法返回的。
子类有自己的__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__()这一行。 -
确实如此。该对象实际上是之前创建的并且使用得很好。我也可以在初始阶段毫无问题地使用 setParent()。然后它被插入到一个列表中,我上面描述的静态方法在列表中“找到”了它,然后将它返回给另一个静态方法。当后者尝试对其使用 setParnet() 时,返回了错误。反正子类的 __init__() 自定义函数是:
-
def __init__(self, image, Father): super(AtomicFactory.Image, self).__init__(father) self.raw_attributes = image.attributes self.attributes = {} Father.addChild(self) self.update()
-
@Martijn Pieters 谢谢,完成
-
你的子类是什么?是
AtomicFactory.Image,还是别的什么?
标签: python pyqt runtime-error superclass deep-copy