Python: How to rebase or dynamically replace a class with a different base class

十年热恋 提交于 2019-12-01 06:46:39
chepner

Make a factory function which dynamically generates the correct base class. In real life, you'd probably memoize the factory so that it always returns the same class object for a given parent, rather than creating multiple identical classes.

from PyQt4 import QtGui, QtCore

def BaseFactory(parent):

    class UiMainBase(parent):
        ....

    return UiMainBase

if __name__ == "__main__":
    import sys
    from PySide import QtGui as PySideGui

    # UiMainBase = BaseFactory(QtGui.QMainWindow)
    UiMainBase = BaseFactory(PySideGui.QMainWindow)
    class MyGui(UiMainBase):
        ...

    Ui = r"C:\pythonTest\ui\test.ui"
    app = PySideGui.QApplication(sys.argv)
    win = MyGui(Ui)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!