gpt4 book ai didi

python - 为什么我不能在 wxPython 中销毁我的 StaticText?

转载 作者:行者123 更新时间:2023-12-01 05:06:15 25 4
gpt4 key购买 nike

我试图从列表中删除静态文本,但收到错误:AttributeError:“tuple”对象没有属性“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
您不能使用 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()

关于python - 为什么我不能在 wxPython 中销毁我的 StaticText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24976694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com