I have a problem with the following code that I don't understand.
Why is the panel1
not expanding?
Thanks.
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()
The line you need to modify is:
sizer_h.Add(st1, 1, wx.EXPAND)
Should be:
sizer_h.Add(panel1, 1, wx.EXPAND)
Because you need to size the panel1 and not only the string.
You want to add the panel to sizer_h, not 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()
You can now see that the panel expands to the full width of its sizer, and the height of the static text, and that the rest is occupied by a blank space. Not too sure how to vertically fill out the panel too so that no grey leaks through, sorry
精彩评论