wxPython StaticBoxSizer

In this tutorial we will discuss the wxPython StaticBoxSizer Layout Sizer.

The wxPython StaticBoxSizer is one of the five sizers in wxPython designed to help with the layout management of widgets in the window. The StaticBoxSizer is just a variant of the BoxSizer, with a single difference.

As the name implies, this Sizer is a combination of the BoxSizer and the StaticBox widget. A StaticBox is required for it’s creation, as you’ll see in the examples below.


wxPython StaticBoxSizer Example

In this example, we’ll demonstrate a simple use of the StaticBoxSizer layout in wxPython.

wrapper = wx.BoxSizer(wx.VERTICAL)

First we created a BoxSizer to act as a wrapper around the StaticBoxSizer. The idea is to use the StaticBoxSizer to create some padding between the widgets in the GridBagSizer and the edges of the window. (Try it both ways, with and without the wrapper).

        sizer = wx.StaticBoxSizer(wx.StaticBox(self.panel, label = "Box"), wx.VERTICAL)

Next we create the StaticBoxSizer, which takes two parameters. The first is a StaticBox widget, which we quickly created. Secondly, we pass in an orientation, which is either wx.VERTICAL or wx.HORIZONTAL. (Just like the BoxSizer Widget). If it’s vertical, then widgets will be stacked vertically and so forth.

        sizer.Add(wx.Button(self.panel, label = "Button 1"), flag = wx.BOTTOM, border = 5)
        sizer.Add(wx.Button(self.panel, label = "Button 2"), flag = wx.BOTTOM, border = 5)
        sizer.Add(wx.Button(self.panel, label = "Button 3"), flag = wx.BOTTOM, border = 5)

Now we add in a bunch of widgets.

        wrapper.Add(sizer, flag = wx.ALL, border = 5)
        self.panel.SetSizer(wrapper)

Finally, we add the StaticBoxSizer into the BoxSizer, and then set the BoxSizer to the panel.


Shown below is the complete code for the StaticBoxSizer example.

import wx

class Window(wx.Frame):
    def __init__(self, title):
        super().__init__(parent = None, title = title)
        self.panel = wx.Panel(self)

        wrapper = wx.BoxSizer(wx.VERTICAL)

        sizer = wx.StaticBoxSizer(wx.StaticBox(self.panel, label = "Box"), wx.VERTICAL)
        sizer.Add(wx.Button(self.panel, label = "Button 1"), flag = wx.BOTTOM, border = 5)
        sizer.Add(wx.Button(self.panel, label = "Button 2"), flag = wx.BOTTOM, border = 5)
        sizer.Add(wx.Button(self.panel, label = "Button 3"), flag = wx.BOTTOM, border = 5)
        
        wrapper.Add(sizer, flag = wx.ALL, border = 5)
        self.panel.SetSizer(wrapper)

        self.Centre()
        self.Show()

        
app = wx.App()
window = Window("WxPython Tutorial")
app.MainLoop()

The output:

wxPython StaticBoxSizer example

Other Sizers in wxPython

There are many other Sizers in wxPython that you can use instead of StaticBoxSizer. Infact, the best layouts are often the result of combining together several different types of Sizers together. This can be done by simply nesting them into each other. There is no limitation to this ability.

  1. BoxSizer
  2. GridSizer
  3. FlexGridSizer
  4. GridBagSizer

This marks the end of the wxPython StaticBoxSizer Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments