python decorator用法_Python:使用decorator更改类类型并保留它的方法

I want to create a class which could be used inside different Applications and their APIs to create UIs. Therefor I created a module called ui.py. Inside this module is the following:

from PyQt4 import QtGui

def CreateGui(uiFile, parent=None):

print "Ui build.."

def GetUiObject(uiClass):

pUI = uiClass.PARENT

class ParentUI(pUI):

def __init__(self, uiFile):

CreateGui(uiFile, self)

def __call__(self, cls):

for func in uiClass.__dict__:

setattr(cls, func, uiClass.__dict__[func])

return ParentUI

@GetUiObject

class UI(object):

PARENT = QtGui.QMainWindow

def __init__(self, uiFile):

CreateGui(uiFile)

Inside my pipeline module module, which is used by the application:

from ui import UI

UI.PARENT = QtGui.QWidget

class Tool_UI(UI):

def __init__(self, uiFile):

super(Tool_UI, self).__init__(uiFile)

print "In Application"

app = QtGui.QApplication(sys.argv)

A = Tool_UI("C:/test/testUi.ui")

A.CreateGui()

But I get the following Error:

Ui build..

In Application

Traceback (most recent call last):

File "C:/test/maxUi.py", line 13, in

A.CreateGui()

RuntimeError: super-class __init__() of type Tool_UI was never called

What do I wrong?

EDIT:

cpburnz answers why the code is erroring, but it is still not working.

I want to replace the class with another one which has a different base. I opened a new question with a better description of my problem and different solutions I tried (How to rebase or dynamically replace a class with a different base class).

解决方案

This looks related to Python: RuntimeError: super-class __init__() of %S was never called but your setup is kind of different so I'll extrapolate to make it more clear.

Traceback (most recent call last):

File "C:/test/maxUi.py", line 13, in

A.CreateGui()

RuntimeError: super-class __init__() of type Tool_UI was never called

This means somewhere in the class hierarchy for Tool_UI that super(...).__init__(...)

is not being called. This appears to be raised by QObject or QWidget.

Looking at the class defined in GetUiObject(), there is no call to the super's init

even though the parent class pUI is QWidget. You most likely need to add

a call to the super init there:

def GetUiObject(uiClass):

pUI = uiClass.PARENT

class ParentUI(pUI):

def __init__(self, uiFile):

super(ParentUI, self).__init__() #

CreateGui(uiFile, self)

def __call__(self, cls):

for func in uiClass.__dict__:

setattr(cls, func, uiClass.__dict__[func])

return ParentUI

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值