2014-07-27 59 views
1

我想從列表中刪除statictext,並得到錯誤:AttributeError: 'tuple' object has no attribute 'Destroy'。我似乎無法找到解決辦法。我的代碼:爲什麼我不能在wxPython中銷燬我的StaticText?

import wx 
class oranges(wx.Frame): 



    def __init__(self,parent,id): 
     wx.Frame.__init__(self,parent,id,'Testing',size=(300,300)) 
     self.frame=wx.Panel(self) 
     subtract=wx.Button(self.frame,label='-',pos=(80,200),size=(30,30)) 
     self.Bind(wx.EVT_BUTTON,self.sub,subtract) 
     self.trying=[] 
     self.something=0 
    def sub(self,event): 
     for i in zip(self.trying): 
      i.Destroy() 
     self.something += 1 
     self.trying.append(wx.StaticText(self.frame,-1,str(self.something),pos=(200,200))) 
     self.trying.append(wx.StaticText(self.frame,-1,str(self.something),pos=(250,200))) 


if __name__ =='__main__': 
    app = wx.PySimpleApp() 
    window = oranges(parent=None,id=-1) 
    window.Show() 
    app.MainLoop() 

我真的很困惑爲什麼StaticText是在一個元組中。非常感謝!期待答案!

+1

您是否閱讀過在代碼中使用的[zip函數](https://docs.python.org/2/library/functions.html#zip)的文檔? – elgonzo

+0

不,我意識到現在不行。有什麼我可以使用的? – user3818089

+1

我不確定我是否理解你最後的評論。爲什麼你認爲你需要用別的東西代替zip()而不是讓for循環遍歷* self.trying *列表? (另外,銷燬StaticText對象後,您需要/需要將它們從* self.trying *列表中刪除...) – elgonzo

回答

2

您只需要for i in self.trying:

但是,如果你摧毀StringText你必須從列表中刪除self.trying太。

def sub(self,event): 

    for i in self.trying: 
     i.Destroy() 
    self.trying = [] # remove all StaticText from list 

    self.something += 1 
    self.trying.append(wx.StaticText(self.frame,-1,str(self.something),pos=(200,200))) 
    self.trying.append(wx.StaticText(self.frame,-1,str(self.something),pos=(250,200))) 

你必須摧毀,再創StaticText?使用SetLabel
你不能改變StaticText文本?

import wx 
class oranges(wx.Frame): 

    def __init__(self,parent,id): 
     wx.Frame.__init__(self,parent,id,'Testing',size=(300,300)) 
     self.frame=wx.Panel(self) 
     subtract=wx.Button(self.frame,label='-',pos=(80,200),size=(30,30)) 
     self.Bind(wx.EVT_BUTTON,self.sub,subtract) 

     self.trying=[] 
     self.trying.append(wx.StaticText(self.frame,-1,'',pos=(200,200))) 
     self.trying.append(wx.StaticText(self.frame,-1,'',pos=(250,200))) 

     self.something=0 

    def sub(self,event): 
     self.something += 1 
     for i in self.trying: 
      i.SetLabel(str(self.something))    

if __name__ =='__main__': 
    app = wx.PySimpleApp() 
    window = oranges(parent=None,id=-1) 
    window.Show() 
    app.MainLoop() 
相關問題