Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to change StaticText label?

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!

like image 501
Myles Avatar asked Apr 12 '13 07:04

Myles


1 Answers

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()
like image 183
Fenikso Avatar answered Oct 14 '22 13:10

Fenikso