如果 ScrolledWindow 的内容确实适合 space,则 wxPython 的 BoxSizer 在 ScrolledWindow 中不起作用

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

我的代码的重要部分是这个:

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]

如果我添加足够的 'Entry' 个对象,所有的小部件都位于左上角;但是如果有足够的小部件,布局就可以了。

另外:如果有自动或至少以更好的方式计算 'SetScrollbars' 高度的好方法,请告诉我!因为这样非常难看。

谢谢!

(我的系统:MacOS,我用的是wxPython phoenix,因为需要用到python3,wxpython-phoenix之类的tag好像没有,如果有请告知我。我想我会就这个主题提出更多问题。)

(代表OP发表).

我解决了。这都是我的错,wxPython 工作正常。我更改了一些值来测试代码,但我忘记了一个。