Search code examples
pythonpython-2.7wxpythontuples

Why can't I destroy my StaticText in wxPython?


I am trying to delete statictext from a list and I get the error: AttributeError: 'tuple' object has no attribute 'Destroy'. I can't seem to find a way around it. My code:

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()

I am really confused on why the StaticText is in a tuple. Thanks so much in advance! Looking forward to the answers!


Solution

  • You need only for i in self.trying:.

    But if you destroy StringText you have to remove it from list self.trying too.

    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)))
    

    Do you have to destroy and create again StaticText ?
    Can't you change text in StaticText using SetLabel ?

    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()