Search code examples
pythonpython-2.7wxpython

How to update wxStatic Text in wxpython?


I am been trying to update wxStaticText without the existing text getting overlapped. I don't have a variable name for each StaticText which is my problem I guess. Full code: http://pastebin.com/Y5k9qEa8

start=70
for i in newfp:
    #Gets info from website
    wx.StaticText(self.frame,-1,str(newname),pos=(50,start))
    self.static = wx.StaticText(self.frame,-1,price,pos=(250,start)) 
    start+=50

Then I have a refresh button that will run that same process again, but if the info changes, it will overlap. I need to know how to make sure it does not overlap. Thanks in advance! Looking forward to the answers!


Solution

  • you need to save a reference to it and use self.my_static_text.SetLabel("Some New Text!")

    ie

    import wx
    messages = ["Hello","World","Python"]
    a = wx.App(redirect=False)
    f = wx.Dialog(None,-1,"Changing Text!")
    te = wx.StaticText(f,-1,"Changes!")
    btn = wx.Button(f,-1,"Change The Text",pos=(10,25))
    btn.Bind(wx.EVT_BUTTON,lambda evt:te.SetLabel(messages.pop(0)) or messages.append(te.GetLabel()))
    f.ShowModal()
    

    in your case you would keep a list

    self.my_items,self.my_prices = [],[]
    items = [("item1",50),("item2",75),("item3",88)]
    for i,(itemName,itemPrice) in enumerate(items):
        self.my_items.append(wx.StaticText(self.frame,-1,itemName,pos=(50,start+i*50)))
        self.my_prices.append(wx.StaticText(self.frame,-1,itemPrice,pos=(250,start+i*50))) 
    

    here is a full example for your use case ... let it also serve as an example of what we want to see from you in terms of a runnable example in future questions

    import wx
    class MyFrame(wx.Frame):
        def __init__(self,items):
            wx.Frame.__init__(self,None,-1,"Demo")
            self.start_y = 25
            self.my_items,self.my_prices = [],[]
            for i,(iName,iPrice) in enumerate(items):
                self.my_items.append(wx.StaticText(self,-1,iName,pos=(50,self.start_y+50*i)))
                self.my_prices.append(wx.StaticText(self,-1,str(iPrice),pos=(90,self.start_y+50*i)))
            btn = wx.Button(self,-1,"Next Items")
            btn.Bind(wx.EVT_BUTTON,self.OnNext)
    
        def OnNext(self,evt):
            for t1,t2 in zip(self.my_items,self.my_prices):
                t1.Destroy()
                t2.Destroy()
            self.my_items,self.my_prices = [],[]
            items = itemSets.pop(0)
            for i,(iName,iPrice) in enumerate(items):
                self.my_items.append(wx.StaticText(self,-1,iName,pos=(50,self.start_y+50*i)))
                self.my_prices.append(wx.StaticText(self,-1,str(iPrice),pos=(90,self.start_y+50*i)))
            itemSets.append(items)
    
    
    itemSets = [
        [("item1",25),("item2",35),("item3",55)],
        [("item3",44),("item4",65),("item5",75)],
        [("item5",66),("item6",78),("item7",93)],
    ]
    a = wx.App(redirect=None)
    f = MyFrame([("ItemA",33),("ItemB",44),("ItemC",66)])
    f.Show()
    a.MainLoop()