Search code examples
macospython-3.xuser-interfacewxpython

wxPython's BoxSizer in a ScrolledWindow doesn't work if the contents of the ScrolledWindow do fit inside the space


The important part of my code is this one:

class Entry(object):
    def __init__(self, list_: 'List', ...) -> None:
        self.list = list_  # type: List
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)  # type: wx.BoxSizer
        self.name = wx.StaticText(list_.panel,  # type: wx.StaticText
                                  label="Example")
        self.sizer.Add(self.name, 1, wx.CENTRE)
        self.widgets = [
            self.name
        ]  # type: List[Union[wx.StaticText, wx.TextCtrl]]
        for i in range(data.columns):
            self.widgets.append(wx.TextCtrl(list_.panel, value="1"))
            self.widgets.append(wx.StaticText(list_.panel, label="1"))
            sizer = wx.BoxSizer(wx.VERTICAL)  # type: wx.BoxSizer
            sizer.Add(self.widgets[-2], 0, wx.EXPAND)
            sizer.Add(self.widgets[-1], 0, wx.EXPAND)
            self.sizer.Add(sizer, 2, wx.EXPAND)
        list_.sizer.Add(self.sizer, 0, wx.EXPAND)
        list_.entries.append(self)
        # ...
        if call_layout:
            height = len(list_.entries) * max(
                i.GetSize()[1]
                for i in self.widgets
            )  # type: int
            list_.sizer.Layout()
            list_.panel.SetScrollbars(4, 4, 1, height)

    # ...


class List(object):
    def __init__(self, ...) -> None:
        # ...
        self.panel = wx.ScrolledWindow(  # type: wx.ScrolledWindow
            design.panel, wx.ID_ANY
        )  # design.panel is a wx.Panel, it's parent is a wx.Frame. The wx.Frame has no sizer, the wx.Panel has a vertical wx.BoxSizer.
        self.sizer = wx.BoxSizer(wx.VERTICAL)  # type: wx.BoxSizer
        design.sizer.Add(self.panel, 1, wx.EXPAND)
        self.panel.SetSizer(self.sizer)

        self.entries = []  # type: List[Entry]

If I add enough 'Entry' objects, all the widgets are located in the top left corner; but the layout works if there are enough widgets.

Additionally: If there is a good way to calculate the height for 'SetScrollbars' automatically or at least in a better way, tell me! Because it is very ugly this way.

Thanks!

(My system: MacOS, I am using wxPython phoenix, because I need to use python3, wxpython-phoenix or another tag like this one doesn't seem to exist, if there is one, tell me. I think I will ask a whole lot more questions on that subject.)


Solution

  • (Posted on behalf of the OP).

    I solved it. It was all my fault, wxPython is working fine. I changed a few values to test the code, but I forgot one.