wxPython StaticBox Widget

In this wxPython Tutorial, we will demonstrate how to use the StaticBox Widget, alongside it’s various styles, features and functions. A complete list of options will be included here, alongside several code examples for your convenience.

Unlike most widgets, the StaticBox needs to be handled a bit differently. Starting from version 2.9, the recommended way to use a StaticBox to group widgets, is to make the widgets children of the StaticBox.

This is similar to how we make widgets children of the Frame or Panel in the Window.

wxPython StaticBox Syntax

The syntax required for making a StaticBox Widget:

StaticBox(parent, id, label, pos,
 size, style, name)
  1. parent: This is the Widget to which it is parented, such as a Panel.
  2. id: Widget ID. Default value is ID_ANY, which gives it the next available ID.
  3. label: The Text which you want to display on the Widget.
  4. pos: A tuple containing the coordinates of where the topleft corner of the StaticBox Widget should begin from.
  5. size: A tuple which defines the dimensions of the area occupied by the Box.
  6. style: Used for styling the StaticBox

Example Code

In the below example we have created a simple StaticBox widget, and two other basic wxPython widgets. We then assign each of those widgets the “StaticBox” as the parent, hence they will appear in it.

Remember to give the StaticBox a size, otherwise the widgets will not appear.

import wx

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

        box = wx.StaticBox(self.panel, label = "StaticBox",
                           size = (200, 200), pos = (20, 20))

        text = wx.StaticText(box, label= "Hello World", pos=(50, 50))
        button = wx.Button(box, label = "Press Me", pos=(50, 100))

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

The output of the above code:

wxPython StaticBox example

Remember! For child widgets of the StaticBox widget, their position is relative to the StaticBox. Passing in (0, 0) into the StaticText widget would place it at the (0, 0) position of the StaticBox, not the window.

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

Notify of
Inline Feedbacks
View all comments