What is the correct way to change StaticText label?

12,327

You have to call either sizer's or parent's Layout() method:

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)
        self.label = wx.StaticText(self.panel, label="Test", style=wx.ALIGN_CENTRE)
        self.button = wx.Button(self.panel, label="Change")

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.label, 1)
        self.sizer.Add(self.button)

        self.button.Bind(wx.EVT_BUTTON, self.OnButton)

        self.panel.SetSizerAndFit(self.sizer)  
        self.Show()

    def OnButton(self, e):
        self.label.SetLabel("Oh, this is very looooong!")
        self.sizer.Layout()
        # self.panel.Layout()  #Either works

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
Share:
12,327

Related videos on Youtube

Myles
Author by

Myles

Updated on September 16, 2022

Comments

  • Myles
    Myles over 1 year

    I am writing a wxPython application, and when I try to change the text in a StaticText object the alignment I have set goes away. It starts off centered, but after the text is changed the alignment goes back to the default, left alignment. Here is my relevant code:

    #initializing
    self.panel = wx.Panel(self)
    self.st_RouteInfo = wx.StaticText(self.panel, label=self.route_start, style=wx.ALIGN_CENTRE)
    
    #changing StaticText
    self.st_RouteInfo.SetLabel("Put text here")
    self.Update()
    

    I'm guessing that I am forgetting something basic, as I am new with wxPython and wxWidgets. Thanks!