2013-03-18 52 views
0

全腦屁中的一類方法這裏甚至不知道我問正確的問題。如何添加/更改類中存在一類的方法?添加類

我建立一個QT GUI中QtDesigner設計。我的Python程序進口,使子類的GUI文件類的新類。我想將方法​​更改爲該類中的按鈕。

所以基本上我下面,我想一個方法添加到「A按鈕」。

qtDesignerFile.py

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

myPythonFile.py

import qtDesignerFile 

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

回答

2

爲了增加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__) 
0
self.aButton.PrintHello = lambda : print "hello!" 

def aMethod(): 
    do_something() 

self.aButton.DoSomething = aMethod 

要麼應該工作...可能更多的方式也......這個假設A按鈕是一個Python類,從Object

繼承