Python: 虚拟类用到弹窗UI界面做继承实现时,报以下错误 :
class Ui_Dialog(QDialog, RunTimeCallBack):
# RunTimeCallBack.class
def notify(self, msgType, postname):
# 更新SN, SOC
print(f"哈哈,PopDialog收到别人的通知了{postname=}")
# RunTimeCallBack.class
def timeUpdate(self, device_index, run_state, startime):
# 更新测试时间
mLogger.debug(f"收到计时器更新 ,{device_index=},{run_state=} ,={startime}")
pass
#。。。。
#。。。。
class RunTimeCallBack(Subscriber):
# def notify(self):
# pass
@abstractmethod
def timeUpdate(self,device_index,run_state,startime): # 用于更新Dialog显示测试时间
pass
class Subscriber(metaclass=ABCMeta):
def __init__(self):
pass
@abstractmethod
def notify(self):
pass
Traceback (most recent call last):
File "/Users/gdlocal/Documents/Lani_work/T03/T03/t03Gui/mainT03HSUi.py", line 5, in <module>
from DeviceItem import T03DeviceWindow
File "/Users/gdlocal/Documents/Lani_work/T03/T03/t03Gui/DeviceItem.py", line 8, in <module>
from PopDialog import Ui_Dialog
File "/Users/gdlocal/Documents/Lani_work/T03/T03/t03Gui/PopDialog.py", line 84, in <module>
class Ui_Dialog(QDialog, RunTimeCallBack):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
原因:
说是元类冲突,
目前暂时的解决方法是:
原来父类,类声明:class Subscriber(metaclass=ABCMeta):
现在修改一下,直接继承object:
class Subscriber(object):
__metaclass__ = ABCMeta #虚拟类的声明在代码块声明,
#class Subscriber(metaclass=ABCMeta):
class Subscriber(object):
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractmethod
def notify(self):
pass
但是
************************************************************************
Python3.0到Python3.3,必须在class语句中使用metaclass=ABCMeta;
在python2中使用时。这个报错,说明定义正确,生效了。
Python2中使用__metaclass__=ABCMeta
***************************************************************
说明上面的__metaclass__ = ABCMeta,没有实现作用。