Python, mixing PyQt5 and abc.ABCMeta

本小妞迷上赌 提交于 2020-01-10 06:04:26

问题


I am trying to create an AbstractClass using both abc.ABCMeta and QObject as parents and cannot seems to make it work.

Here is the Base class init. I have Pyqt5 and python 2.7

pyqtWrapperType = type(QObject)

class ParamsHandler(abc.ABCMeta, pyqtWrapperType):
    def __init__(self, device_model, read_only=False):
        super(ParamsHandler, self).__init__()
        self.cmd_to_get_data = None
        self.device_model = device_model

class ConfigParamsHandler(ParamsHandler):
    def __init__(self, device_model):
        super(ConfigParamsHandler, self).__init__(device_model)
        self.cmd_to_get_data = Commands.CONFIG_PARAMS

I get a TypeError('new() takes exactly 4 arguments (2 given)',) I also have Pycharm suggesting that I use cls instead of self

If I supply 4 arguments, it asks for a string as the first argument.


回答1:


I solved it using this approach instead:

class ParamsHandler_Meta(type(QObject), type(abc.ABCMeta)):
    pass
class ParamsHandlerClass(QObject):
    pass
class ParamsHandler(ParamsHandlerClass):
    __metaclass__ = ParamsHandler_Meta
    def __init__(self, device_model, cmd_to_get_data, read_only=False):
        super(ParamsHandler, self).__init__()
        self.cmd_to_get_data = cmd_to_get_data



回答2:


abc.ABCMeta is supposed to be used as a metaclass. So instead of extending it, try:

class ParamsHandler(pyqtWrapperType):
    __metaclass__ = abc.ABCMeta


来源:https://stackoverflow.com/questions/49384161/python-mixing-pyqt5-and-abc-abcmeta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!