Python 向类中的类添加方法

Python 向类中的类添加方法,python,Python,这里完全是大脑放屁,甚至不确定我问的是正确的问题。如何添加/更改类中存在的类的方法 我正在构建一个用QtDesigner设计的QTGUI。我的Python程序导入一个新类并将其子类化为GUI文件类。我想将一个方法更改为该类中的按钮 基本上我有下面的内容,我想给“aButton”添加一个方法 qtDesignerFile.py class Ui_MainWindow(object): def setupUi(self, MainWindow): self.aButton =

这里完全是大脑放屁,甚至不确定我问的是正确的问题。如何添加/更改类中存在的类的方法

我正在构建一个用QtDesigner设计的QTGUI。我的Python程序导入一个新类并将其子类化为GUI文件类。我想将一个方法更改为该类中的按钮

基本上我有下面的内容,我想给“aButton”添加一个方法

qtDesignerFile.py

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.aButton = QtGui.QPushButton()
import qtDesignerFile

class slidingAppView(QMainWindow,slidingGuiUi.Ui_MainWindow):
    def __init__(self,parent=None):
        super(slidingAppView,self).__init__(parent)
myPythonFile.py

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.aButton = QtGui.QPushButton()
import qtDesignerFile

class slidingAppView(QMainWindow,slidingGuiUi.Ui_MainWindow):
    def __init__(self,parent=None):
        super(slidingAppView,self).__init__(parent)


两者中的任何一个都应该起作用。。。也许还有更多的方法。。。这假设aButton是一个python类,继承自Object,添加到Joran的答案中,方法如下:

def foo():
    pass

instance.foo = foo
将类似于静态方法(它们不会将实例作为第一个参数传递)。如果要添加绑定方法,可以执行以下操作:

from types import MethodType

def foo(instance):
    # this function will receive the instance as first argument
    # similar to a bound method
    pass

instance.foo = MethodType(foo, instance, instance.__class__)