pyqt4 python中的MRO错误
作者:互联网
我想继承widget1以使用其方法,但得到:
"TypeError: Error when calling the metaclass bases Cannot create a
consistent method resolution order (MRO) for bases widget1, QWidget"
当我运行程序时.你能告诉我为什么会这样吗?
谢谢.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtCore, QtGui
import sys
class widget1(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
class widget2(QtGui.QWidget, widget1):
def __init__(self):
QtGui.QWidget.__init__(self)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
test = widget1()
test.show()
sys.exit(app.exec_())
解决方法:
It is not possible to define a new Python class that sub-classes from
more than one Qt class.
您可以使用多个替代设计决策,从而无需多个QObject继承.
从单父类简单继承
class widget1(QtGui.QWidget):
def __init__(self):
super(widget1, self).__init__()
def foo(self): pass
def bar(self): pass
class widget2(widget1):
def __init__(self):
super(widget2, self).__init__()
def foo(self): print "foo"
def baz(self): pass
组成
class widget2(QtGui.QWidget):
def __init__(self):
super(widget2, self).__init__()
self.widget1 = widget1()
使其中一个类成为mixin类,而不是QObject:
class widget1(QtGui.QWidget):
def __init__(self):
super(widget1, self).__init__()
def foo(self): print "foo"
def bar(self): pass
class MixinClass(object):
def someMethod(self):
print "FOO"
class widget2(widget1, MixinClass):
def __init__(self):
super(widget2, self).__init__()
def bar(self): self.foo()
def baz(self): self.someMethod()
标签:pyqt4,python 来源: https://codeday.me/bug/20191201/2078351.html