WxPython:在面板中嵌套面板

Howtousernamepls

我一直在尝试学习wxPython,只是无法理解大小调整器。作为练习,我试图布置三个面板,使它们彼此出现。首先,我使用了图像编辑程序来显示我要实现的近似窗口:

这是我用来尝试的代码:

import wx


class MyApp(wx.Frame):

    def __init__(self):
        super().__init__(None, title="Test")

        outer_sizer = wx.BoxSizer()
        outer_panel = wx.Panel(self)
        outer_panel.SetBackgroundColour(wx.Colour("#94f7e7"))

        inner_panel = wx.Panel(outer_panel)
        inner_panel.SetBackgroundColour(wx.Colour("#b8e8a9"))
        inner_sizer = wx.BoxSizer()

        inner_inner_panel = wx.Panel(inner_panel)
        inner_inner_panel.SetBackgroundColour(wx.Colour("#cbebf5"))
        inner_inner_sizer = wx.BoxSizer()
        inner_inner_sizer.Add(inner_inner_panel, 1, wx.ALL | wx.EXPAND, 20)

        inner_sizer.Add(inner_inner_panel, 1, wx.ALL | wx.EXPAND, 20)

        outer_sizer.Add(inner_sizer, 1, wx.EXPAND)

        outer_panel.SetSizer(outer_sizer)


if __name__ == '__main__':
    app = wx.App()
    ma = MyApp()
    ma.Show()
    app.MainLoop()

这是它实际产生的结果:

我希望上面的代码能工作的原因是因为我在以下代码上取得一些成功:

import wx


class MyApp(wx.Frame):

    def __init__(self):
        super().__init__(None, title="Test")

        outer_sizer = wx.BoxSizer()
        outer_panel = wx.Panel(self)
        outer_panel.SetBackgroundColour(wx.Colour("#94f7e7"))

        inner_panel = wx.Panel(outer_panel)
        inner_panel.SetBackgroundColour(wx.Colour("#b8e8a9"))
        inner_sizer = wx.BoxSizer()

        inner_sizer.Add(inner_panel, 1, wx.ALL | wx.EXPAND, 20)

        outer_sizer.Add(inner_sizer, 1, wx.EXPAND)

        outer_panel.SetSizer(outer_sizer)


if __name__ == '__main__':
    app = wx.App()
    ma = MyApp()
    ma.Show()
    app.MainLoop()

哪个产生了这个:

现在显然我没有得到任何东西。我希望以上内容能够使某种友善的人识别出我不了解的内容。

None of the related posts here seem to help much as they don't ever nest panels more than one layer.

Petr Blahos

Ok, the first step will be putting in just one panel and make a hierarchy of 3 sizers. This works exactly as you expect, only you cannot change the background color of a sizer:

def __init__(self):
    super().__init__(None, title="Test")

    sz1 = wx.BoxSizer()
    sz2 = wx.BoxSizer()
    sz3 = wx.BoxSizer()

    p1 = wx.Panel(self)
    p1.SetBackgroundColour(wx.RED)

    sz3.Add(p1, 1, wx.ALL | wx.EXPAND, 20)
    sz2.Add(sz3, 1, wx.ALL | wx.EXPAND, 20)
    sz1.Add(sz2, 1, wx.ALL | wx.EXPAND, 20)

    self.SetSizer(sz1)

Now, to really use the panels you have to put the panel into sizer (sizer.Add) and than the sizer into the panel (panel.SetSizer) and than into a sizer and so on... Just be careful about setting the right parent.

def __init__(self):
    super().__init__(None, title="Test")

    sz1 = wx.BoxSizer()
    sz2 = wx.BoxSizer()
    sz3 = wx.BoxSizer()

    p1 = wx.Panel(self)
    p2 = wx.Panel(p1)
    p3 = wx.Panel(p2)

    p1.SetBackgroundColour(wx.RED)
    p2.SetBackgroundColour(wx.GREEN)
    p3.SetBackgroundColour(wx.BLUE)

    sz3.Add(p3, 1, wx.ALL | wx.EXPAND, 20)  # innermost
    p2.SetSizer(sz3)
    sz2.Add(p2, 1, wx.ALL | wx.EXPAND, 20)
    p1.SetSizer(sz2)
    sz1.Add(p1, 1, wx.ALL | wx.EXPAND, 0)

    self.SetSizer(sz1)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章