我对以下代码有疑问,我不明白。
为什么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()
答案 0 :(得分:2)
您需要修改的行是:
sizer_h.Add(st1, 1, wx.EXPAND)
应该是:
sizer_h.Add(panel1, 1, wx.EXPAND)
因为您需要调整panel1的大小,而不仅仅是字符串。
答案 1 :(得分: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()
您现在可以看到面板扩展到其大小调整器的整个宽度,以及静态文本的高度,其余部分由空格占用。不太确定如何垂直填充面板,以便没有灰色泄漏,抱歉