ListCtrl inside a StaticBox - messed up layout

Hi there!

I am having a problem putting a wx.ListCtrl inside a wx.StaticBox. I have a panel with two lists side-by-side, which displays fine, but then I wanted to put a border around each with a descriptive text, and apparently that can be done by using a StaticBox and putting the control inside.

However, as you can see from the screenshot, the list on the left, which has been placed inside a StaticBox, does not stay inside the box, nor is it sized properly. (Without the box, it fills the left half of its sizer, like the list on the right).

This is the code for building the UI (self here is a wx.Frame):

    def init_ui(self):
        self.panel1 = wx.Panel(self)
        self.text1 = wx.TextCtrl(self.panel1)
        self.text2 = wx.TextCtrl(self.panel1)

        # Set up Groups List
        self.sbox1 = wx.StaticBox(self.panel1, wx.ID_ANY, "Groups")
        self.list1 = wx.ListCtrl(self.sbox1, id=wx.ID_ANY, style=wx.LC_REPORT)
        self.list1.InsertColumn(2, 'Group')
        self.list1.InsertColumn(3, 'Description')

        # Codes List
        self.list2 = wx.ListCtrl(self.panel1, id=wx.ID_ANY, style=wx.LC_REPORT)
        self.list2.InsertColumn(2, 'Code')
        self.list2.InsertColumn(3, 'Description')

        self.sizer0 = wx.BoxSizer(wx.VERTICAL)
        self.sizer1 = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)

        self.panel1.SetSizer(self.sizer0)

        self.sizer0.Add(self.sizer2, 0, flag=wx.ALL | wx.EXPAND, border=5)
        self.sizer0.Add(self.sizer1, 1, flag=wx.ALL | wx.EXPAND, border=5)

        self.sizer2.Add(self.text1, 0, flag=wx.ALL, border=5)
        self.sizer2.Add(self.text2, 0, flag=wx.ALL, border=5)

        self.sizer1.Add(self.sbox1, 1, flag=wx.ALL | wx.EXPAND, border=5)
        self.sizer1.Add(self.list2, 1, flag=wx.ALL | wx.EXPAND, border=5)

I managed to resolve the issue by using a wx.StaticBoxSizer instead, and adding the list to it, with the usual flags and size specification.

But what is the purpose then of the StaticBox?

I spent quite a lot of time today experimenting with just using wx.StaticBox and not wx.StaticBoxSizer just to see if the required layout was possible.

The closest I got was by putting a wx.BoxSizer inside the wx.StaticBox and adding self.list1 to the wx.BoxSizer. The only problem was that, no matter what I tried, I couldn’t get a gap between the bottom of self.list1 and the border of the wx.StaticBox:

I searched in GitHub and found the following issue in the wxWidgets repository which may be related:

That issue is still open and doesn’t appear to have been addressed.

I am using wxPython 4.2.1 gtk3 (phoenix) wxWidgets 3.2.4 + Python 3.12.3 + Linux Mint 22

1 Like

With a box sizer, you would have the border at the bottom, right?
What is then the point of avoiding the box sizer?
grafik

If you want this to work without the sizer, you probably have to fix it yourself.
What happens if you set the font size of the box to the minimum? Maybe the size calculation does not take into account the height of the label.

1 Like

A StaticBoxSizer simply makes using a StaticBox easier. It needs a StaticBox to work with. You can either create a StaticBox and pass it to the StaticBoxSizer constructor or, if you use the overloaded StaticBoxSizer constructor, it will create a StaticBox ‘on the fly’.

1 Like

My experiments were based on the StaticBox example in the wxPython Demo which puts a StaticText in a BoxSizer and then passes the BoxSizer to the StaticBox’s SetSizer() method.

Instead of the StaticText I used a ListCtrl. I did set a bottom border for the ListCtrl and also tried adding a spacer below the ListCtrl, but neither of these produced a gap between the ListCtrl and the border of the StaticBox. I also tried using a multiline TextCtrl instead of the ListCtrl, but the result was the same.

I tried your suggestion of reducing the StaticBox’s font size and found that when the font’s pointSize was set to 4, a small gap did appear! Reducing the point size further increased the gap (but by then of course the text on the StaticBox and the column headings was unreadable).

Just to be clear - if I wanted to use a StaticBox in an actual application, I would use a StaticBoxSizer - my experiments were just done out of academic interest.

1 Like

Thanks for the feedback. Perhaps I did not understand the documentation correctly. I thought StatixBox was the preferred way to go.

It must be common and much easier to use StaticBoxSizer combind with StaticBox as you noted. So, I suspect this issue has been forgotten.
Anyway, I believe the OP’s problem can be solved by using StaticBoxSizer.