2010-06-30 39 views
2

我遇到以下代碼的問題,我不明白。面板不在wxPython中展開

爲什麼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() 

回答

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() 

您現在可以看到,該面板將展開其分級機的整個寬度,和靜態文本的高度,而其餘部分由一個空格所佔據。也不太清楚如何垂直填寫面板也讓沒有灰色泄漏通過了,不好意思

2

您需要修改該生產線是:

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

應該是:

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

因爲您需要調整panel1的大小而不僅僅是字符串。