2

我对以下代码有疑问,我不明白。

为什么panel1不展开?

谢谢。

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
        panel1 = wx.Panel(self)
        panel2 = wx.Panel(self)
        panel3 = wx.Panel(self)

        panel1.SetBackgroundColour("green")
        panel2.SetBackgroundColour("yellow")
        panel3.SetBackgroundColour("red")

        sizer_h = wx.BoxSizer(wx.HORIZONTAL)
        sizer_v = wx.BoxSizer(wx.VERTICAL)

        st1 = wx.StaticText(panel1, -1, "TEST")
        sizer_h.Add(st1, 1, wx.EXPAND)
        sizer_v.Add(sizer_h, proportion=1, flag=wx.EXPAND)
        sizer_v.Add(panel2, proportion=2, flag=wx.EXPAND)
        sizer_v.Add(panel3, proportion=1, flag=wx.EXPAND)
        # only set the main sizer if you have more than one
        self.SetSizer(sizer_v)

app = wx.App()
mytitle = "wx.Frame & wx.Panels"
width = 300
height = 320
MyFrame(None, mytitle, (width, height)).Show()
app.MainLoop()
4

2 回答 2

2

您需要修改的行是:

sizer_h.Add(st1, 1, wx.EXPAND)

应该:

sizer_h.Add(panel1, 1, wx.EXPAND)

因为您需要调整 panel1 的大小而不仅仅是字符串。

于 2010-07-01T23:45:32.760 回答
1

您想将面板添加到 sizer_h,而不是 st1。

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
        panel1 = wx.Panel(self)
        panel2 = wx.Panel(self)
        panel3 = wx.Panel(self)

        panel1.SetBackgroundColour("green")
        panel2.SetBackgroundColour("yellow")
        panel3.SetBackgroundColour("red")

        sizer_h = wx.BoxSizer(wx.HORIZONTAL)
        sizer_v = wx.BoxSizer(wx.VERTICAL)

        st1 = wx.StaticText(panel1, -1, "TEST")
        sizer_h.Add(panel1, wx.EXPAND)
        sizer_v.Add(sizer_h, proportion=1, flag=wx.EXPAND)
        sizer_v.Add(panel2, proportion=2, flag=wx.EXPAND)
        sizer_v.Add(panel3, proportion=1, flag=wx.EXPAND)
        # only set the main sizer if you have more than one
        self.SetSizer(sizer_v)

app = wx.App()
mytitle = "wx.Frame & wx.Panels"
width = 300
height = 320
MyFrame(None, mytitle, (width, height)).Show()
app.MainLoop()

您现在可以看到面板扩展到其大小的全宽和静态文本的高度,其余部分被空白空间占据。不太清楚如何垂直填充面板,以免灰色泄漏,抱歉

于 2010-07-01T19:56:07.163 回答