wxPython Sizers Tutorial – Layout Management

In this tutorial we will take a look at Layout Management in wxPython, and explain we can use advanced features and sizers to correctly position widgets inside the wxPython window.

Instead of “Absolute Positioning”, we will use wxPython’s “Sizers”, which were designed to help place widgets in a specific manner/layout. There are a total of 5 sizers in wxPython, each discussed briefly here with an example.

For more detailed tutorials with complete lists of styles/options, refer to the individual tutorials for each Sizer by following the links.


Absolute Positioning

Absolute positioning, also known as fixed positioning is the use of the coordinate system when placing widgets. This results in a “fixed position” of the widget, which will not adapt to any changes in the screen size.

Furthermore, making changes to the layout with absolute positioning is trickier once you have alot of widgets in your window. On the other hand, with Sizers you just need to change the Sizer’s settings, and all the widgets in it will be effected as well.

Another major advantage of Sizers, is that you can place a Sizer within another, allowing for complex layouts patterns.

A small example using absolute positioning.

import wx

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

        text = wx.StaticText(self.panel, label= "Hello World", pos=(50, 50))
        button = wx.Button(self.panel, label = "Press Me", pos=(200, 50))
        rb = wx.RadioButton(self.panel, label = "Pizza", pos=(50, 120))
        cb = wx.ComboBox(self.panel, value = "Kittens", pos=(200, 120))

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

The output: