Python3/PyQt中的多重继承顺序 [英] Multiple inheritance order in Python3/PyQt

查看:186
本文介绍了Python3/PyQt中的多重继承顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与PyQt一起使用多重继承时遇到了一个问题,PyQt是 Programe 1#源代码,如下所示:

I met an issue when using multi inheritance with PyQt, the Programe 1# source code as below:

#!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'

如果更改Widget_C定义 来自

class Widget_C(WP_Widget, WP_Line):

class Widget_C(WP_Line, WP_Widget):

它将成功运行.

我想它与Python3中的 MRO 有关,所以我编写了另一个程序2#来模拟状态:

I guess it will be related to MRO in Python3, so I write another program 2# to simulate the state:

#!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是哪个继承顺序,

But no matter which inheriting sequence of Wiget_C,

class Widget_C(WP_Line, WP_Widget):

class Widget_C(WP_Widget, WP_Line):

它们都将正常运行.

任何人都可以帮助:

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

Python和多重继承中的方法顺序解释关于 MRO 的一些信息,它与我的问题有关,但与答案并不完全相同. 如果继承顺序相同,则我的程序1#程序2 不应有不同的结果,所以关键是为什么程序1#程序2 的现象不同.

Python and order of methods in multiple inheritance explains something about MRO, it related to my question, but not exactly the answer. 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.

推荐答案

ekhumoro 给出了我所需要的确切答案.

ekhumoro gave the exactly answer what I need.

文档 中所述,pyqt不会支持qt类的多重继承.就是说,它不会像您在普通python中通常期望的那样工作.

As stated in the docs, pyqt does not support multiple inheritance of qt classes. Which is to say, it won't work the way you would normally expect it to in normal python.

话虽如此,此博客文章有一些有趣的见解和变通方法(但请注意,它最初是为pyqt4编写的,因此某些事情现在可能已经过时了).

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

这篇关于Python3/PyQt中的多重继承顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆