2013-07-19 65 views
-1

我想調用另一個類中的現有實例到Python中的其他類中,但我不知道該怎麼做,請幫忙!在Python中檢索現有的實例

class ButtonSelectRetire(object): 

    def __init__(self): 
     self.enabled = True 
     self.checked = False 
    def onClick(self):   

     cbr = ButtontRetire() 
     cbr.off()  

class ButtontRetire(object): 

    def __init__(self): 
     self.enabled = True 
     self.checked = False 

    def onClick(self): 
     pass 

    def on (self): 
     self.enabled = True 

    def off (self): 
     self.enabled = False 

回答

0

也許你可以傳遞的ButtonRetire一個實例的ButtonSelectRetire構造:

class ButtonSelectRetire(object): 

    def __init__(self, button_retire): 
    self.enabled = True 
    self.checked = False 
    self.button_retire = button_retire 

    def onClick(self):   
    self.button_retire.off() 


>>> buttonRetire = ButtonRetire() 
>>> buttonSelectRetire = ButtonSelectRetire(buttonRetire) 

其實,據我瞭解,你要連接的信號buttonSelectRetire.onClick回調buttonRetire.off()

如果你是在一個PyQt4的程序,這可以很容易做到:

self.buttonSelectRetire.onClick.connect(self.buttonRetire.off) 
+0

再次感謝您Frodon!我嘗試過,但它給了我一個未綁定的錯誤, – CesarD

+0

在onClick self.ButtontRetire.onf() TypeError:unbound方法onf()必須調用ButtontRetire實例作爲第一個參數(沒有任何代替) – CesarD

+0

我編輯我的答案演示如何創建實例。你是怎麼做的? – Frodon