Python3/PyQt 中的多重继承顺序
Multiple inheritance order in Python3/PyQt
我在PyQt中使用多重继承时遇到一个问题,程序1#源代码如下:
#!python3
import sys;
from PyQt5.QtWidgets import *;
from PyQt5.QtGui import *;
from PyQt5.QtCore import *;
class WP_Widget(QWidget):
def __init__(self):
print("WP_Widget init");
super().__init__();
class WP_Line(QLineEdit):
def __init__(self, text='',*args, **kargs):
super().__init__();
self.setText(text);
class Widget_C(WP_Widget, WP_Line):
#class Widget_C(WP_Line, WP_Widget):
def __init__(self):
print('Widget_C self = ', self)
super().__init__();
class App(QWidget):
def __init__(self):
super().__init__();
fname = Widget_C();
self.left = 100
self.top = 100
self.width = 100
self.height = 100
self.show();
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
执行时会报错:
AttributeError: 'Widget_C' object has no attribute 'setText'
如果更改 Widget_C
定义
来自
class Widget_C(WP_Widget, WP_Line):
至
class Widget_C(WP_Line, WP_Widget):
会运行成功。
估计会和Python3中的MRO有关,所以又写了一个程序2#来模拟状态:
#!python3
class QWidget():
def __init__(self):
print("Base QWidget init.");
class QLineEdit(QWidget):
def __init__(self):
print('LineEdit init');
super().__init__();
def setText(self, text):
print('setText called');
class WP_Widget(QWidget):
def __init__(self):
print('WP_Widget Init');
super().__init__()
class WP_Line(QLineEdit):
def __init__(self, text='',*args, **kargs):
print('WP_Line init');
super().__init__();
self.setText(text)
class Widget_C(WP_Widget, WP_Line):
#class Widget_C(WP_Line, WP_Widget):
def __init__(self):
super().__init__()
c_test = Widget_C()
但无论Wiget_C
、
是哪个继承序列
class Widget_C(WP_Line, WP_Widget):
或
class Widget_C(WP_Widget, WP_Line):
两者都会运行正常。
所以有人可以帮忙吗:
- 解释为什么 程序 1# 在定义为
class Widget_C(WP_Widget, WP_Line):
时失败,MRO 只是我的猜测。
- 为什么程序2#在两种情况下都可以正常运行?
- 帮忙修改程序2#重现程序1#的状态.
Python and order of methods in multiple inheritance 解释了一些关于 MRO 的内容,它与我的问题有关,但不完全是答案。
如果继承顺序相同,我的程序1#和程序2应该不会有不同的结果,关键是为什么程序1#和程序2现象不同
ekhumoro 给出了我所需要的准确答案。
同stated in the docs一样,pyqt不支持qt的多重继承类。也就是说,在 python 中,它不会像您通常期望的那样工作。
话虽如此,this blog post 有一些有趣的见解和解决方法(但请注意,它最初是为 pyqt4 编写的,所以有些东西现在可能已经过时了)。
我在PyQt中使用多重继承时遇到一个问题,程序1#源代码如下:
#!python3
import sys;
from PyQt5.QtWidgets import *;
from PyQt5.QtGui import *;
from PyQt5.QtCore import *;
class WP_Widget(QWidget):
def __init__(self):
print("WP_Widget init");
super().__init__();
class WP_Line(QLineEdit):
def __init__(self, text='',*args, **kargs):
super().__init__();
self.setText(text);
class Widget_C(WP_Widget, WP_Line):
#class Widget_C(WP_Line, WP_Widget):
def __init__(self):
print('Widget_C self = ', self)
super().__init__();
class App(QWidget):
def __init__(self):
super().__init__();
fname = Widget_C();
self.left = 100
self.top = 100
self.width = 100
self.height = 100
self.show();
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
执行时会报错:
AttributeError: 'Widget_C' object has no attribute 'setText'
如果更改 Widget_C
定义
来自
class Widget_C(WP_Widget, WP_Line):
至
class Widget_C(WP_Line, WP_Widget):
会运行成功。
估计会和Python3中的MRO有关,所以又写了一个程序2#来模拟状态:
#!python3
class QWidget():
def __init__(self):
print("Base QWidget init.");
class QLineEdit(QWidget):
def __init__(self):
print('LineEdit init');
super().__init__();
def setText(self, text):
print('setText called');
class WP_Widget(QWidget):
def __init__(self):
print('WP_Widget Init');
super().__init__()
class WP_Line(QLineEdit):
def __init__(self, text='',*args, **kargs):
print('WP_Line init');
super().__init__();
self.setText(text)
class Widget_C(WP_Widget, WP_Line):
#class Widget_C(WP_Line, WP_Widget):
def __init__(self):
super().__init__()
c_test = Widget_C()
但无论Wiget_C
、
class Widget_C(WP_Line, WP_Widget):
或
class Widget_C(WP_Widget, WP_Line):
两者都会运行正常。
所以有人可以帮忙吗:
- 解释为什么 程序 1# 在定义为
class Widget_C(WP_Widget, WP_Line):
时失败,MRO 只是我的猜测。 - 为什么程序2#在两种情况下都可以正常运行?
- 帮忙修改程序2#重现程序1#的状态.
Python and order of methods in multiple inheritance 解释了一些关于 MRO 的内容,它与我的问题有关,但不完全是答案。 如果继承顺序相同,我的程序1#和程序2应该不会有不同的结果,关键是为什么程序1#和程序2现象不同
ekhumoro 给出了我所需要的准确答案。
同stated in the docs一样,pyqt不支持qt的多重继承类。也就是说,在 python 中,它不会像您通常期望的那样工作。
话虽如此,this blog post 有一些有趣的见解和解决方法(但请注意,它最初是为 pyqt4 编写的,所以有些东西现在可能已经过时了)。