【问题标题】:Why can't I destroy my StaticText in wxPython?为什么我不能在 wxPython 中销毁我的 StaticText?
【发布时间】:2014-07-27 00:13:15
【问题描述】:

我试图从列表中删除静态文本,但出现错误: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 在一个元组中。提前非常感谢!期待答案!

【问题讨论】:

  • 您是否阅读了您在代码中使用的zip function 的文档?
  • 不,我知道现在这行不通。还有什么我可以用的吗?
  • 我不确定我是否理解您的最后评论。为什么你认为你需要用其他东西代替 zip() 而不是让 for 循环简单地遍历 self.trying 列表? (此外,在销毁 StaticText 对象后,您希望/需要将它们从 self.trying 列表中删除...)
  • 用你自己的话来说,你为什么首先在那里使用zip?它应该完成什么?

标签: python python-2.7 wxpython tuples


【解决方案1】:

您只需要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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 2015-04-24
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多