我正在尝试从列表中删除statictext,我收到错误:AttributeError: 'tuple' object has no attribute 'Destroy'
。我似乎无法找到解决办法。我的代码:
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在一个元组中。非常感谢提前!期待着答案!
答案 0 :(得分: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
?
您无法使用StaticText
更改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()