Search code examples
wxpython

Modify StaticBoxSizer label on wxPython


Is there a way of modifying the label of a StaticBoxSizer on wxPython after initialization?

I couldn't find anything on wxPython's documentation.

Thank you


Solution

  • When you create a wx.StaticBoxSizer, you must pass it a wx.StaticBox as the first argument of the initializer, this is what you need to modify to change the label. If you look at the class hierarchies, they go as follows:

    • object -> Object -> EvtHandler -> Window -> Control -> StaticBox
    • object -> Object -> Sizer -> BoxSizer -> StaticBoxSizer

    As you may have figured out, SetLabel is not a method of the sizer or any of it's parents, it instead lives in the Control class, so the box inherits it.

    # creating the static box sizer
    self.my_box = wx.StaticBox(self.panel, wx.ID_ANY, "Spam, spam, spam")
    self.sizer_static_box = wx.StaticBoxSizer(self.my_box)
    
    # then do something like this later        
    self.my_box.SetLabel("I hate spam!")