What is the correct way to change the StaticText label?

I am writing a wxPython application, and when I try to change the text in a StaticText object, the alignment I set goes away. It starts from the center, but after changing the text, the alignment returns to the default alignment, on the left. Here is my 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 assume that I am forgetting something basic, as I am new to wxPython and wxWidgets. Thanks!

+4
source share
1 answer

You must call either the sizer or the parent 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() 
+6
source

All Articles