简体   繁体   English

Python3 / PyQt中的多重继承顺序

[英]Multiple inheritance order in Python3/PyQt

I met an issue when using multi inheritance with PyQt, the Programe 1# source code as below: 当与PyQt一起使用多重继承时,遇到了一个问题, Programy 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_())

When execute, it will show the error as: 执行时,它将显示错误为:

AttributeError: 'Widget_C' object has no attribute 'setText'

If change Widget_C definition from 如果将Widget_C定义从

class Widget_C(WP_Widget, WP_Line):

to

class Widget_C(WP_Line, WP_Widget):

It will run successfully. 它将成功运行。

I guess it will be related to MRO in Python3, so I write another program 2# to simulate the state: 我想这将与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()

But no matter which inheriting sequence of Wiget_C , 但是无论Wiget_C继承顺序Wiget_C

class Widget_C(WP_Line, WP_Widget):

or 要么

class Widget_C(WP_Widget, WP_Line):

both of them will run normally. 它们都将正常运行。

So could anyone help: 所以任何人都可以帮助:

  1. Explain why program 1# fails when defined as class Widget_C(WP_Widget, WP_Line): , MRO is just my guess. 解释为什么程序1#在定义为class Widget_C(WP_Widget, WP_Line):时失败class Widget_C(WP_Widget, WP_Line): MRO只是我的猜测。
  2. Why program 2# can be run normally in both condition ? 为什么程序2#在两种情况下都可以正常运行?
  3. Help modify program 2# to reproduce the state of program 1# . 帮助修改程序2#以重现程序1#的状态。

Python and order of methods in multiple inheritance explains something about MRO , it related to my question, but not exactly the answer. Python和多重继承中的方法顺序解释了有关MRO的某些信息,它与我的问题有关,但与答案不完全相同。 If inheriting order is the same, my program 1# and program 2 should not have different results, so the key point is why program 1# and program 2 have different phenomenon. 如果继承顺序相同,则我的程序1#程序2不应有不同的结果,所以关键是为什么程序1#程序2会有不同的现象。

ekhumoro gave the exactly answer what I need. ekhumoro给出了我所需要的确切答案。

As stated in the docs , pyqt does not support multiple inheritance of qt classes. docs中所述 ,pyqt不支持qt类的多重继承。 Which is to say, it won't work the way you would normally expect it to in normal python. 就是说,它不会像通常的python那样正常工作。

Having said that, this blog post has some interesting insights and work-arounds (but note that it was originally written for pyqt4, so some things may now be out of date). 话虽如此, 这篇博客文章还是有一些有趣的见解和变通方法(但请注意,它最初是为pyqt4编写的,因此有些事情可能已经过时了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM