2012-06-07 15 views
0

我正在爲GUI使用wxPython編寫一個計算器。我做了一個名爲display的類來使用StaticText來顯示文本。無論如何,當我嘗試更新屏幕時,會引發異常。 下面的代碼:爲什麼我無法更新wxPython的StaticText?

class display: 
    def __init__(self,parent, id): 
     print "display class is working" 
     global string1 
     self.view = wx.StaticText(frame, -1, "Waiting", (30,7), style = wx.ALIGN_CENTRE) 

    @staticmethod 
    def update(self): 
     global string1 
     self.view.SetLabel(string1) 

每當我嘗試運行update()函數,它會引發此異常:

AttributeError: 'function' object has no attribute 'view' 

當我寫 「self.view = WX等等等等」,我試圖將StaticText設置爲變量名,所以我可以使用SetLabel函數。文本似乎工作,直到我嘗試更新它。爲什麼我不能更新它?我如何解決它?

回答

0

@staticmethods沒有參數...所以它實際上沒有得到自我......你需要要麼使之成爲@classmethod它得到CLS或者你只是需要使它成爲一個正常的方法

class display: 
    view = None 
    def __init__(self,parent, id): 
     print "display class is working" 
     global string1 
     display.view = wx.StaticText(frame, -1, "Waiting", (30,7), style = wx.ALIGN_CENTRE) 

    @classmethod 
    def update(cls): 
     global string1 
     cls.view.SetLabel(string1) 
相關問題