我试图从列表中删除静态文本,但出现错误: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是元组感到困惑。非常感谢!期待答案!
最佳答案
您只需要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()
关于python - 为什么我不能在wxPython中销毁我的StaticText?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24976694/