gpt4 book ai didi

python - pyqt4 python中的MRO错误

转载 作者:行者123 更新时间:2023-11-28 16:48:24 24 4
gpt4 key购买 nike

我想继承 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_())

最佳答案

Multiple Inheritance in PyQt4

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()

使其中一个类成为混合类,它不是 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()

关于python - pyqt4 python中的MRO错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11322053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com