2016-03-08 27 views
0

我有一個有6個網格如下畫面:wxPython的分級機擴展問題

enter image description here

我加入第7格。 這使得屏幕看起來像:

enter image description here

但是我希望屏幕是:

enter image description here

我試着用的FlexGrid做到這一點,但它不工作。

這是我的代碼:

sizer_2 = wx.BoxSizer(wx.HORIZONTAL) 
sizer_2.AddSpacer(10) 

sizer_4 = wx.BoxSizer(wx.VERTICAL) 
sizer_4.Add(self.grid1, 1, wx.EXPAND, 0) 
sizer_4.AddSpacer(20) 
sizer_4.Add(self.grid4, 1, wx.EXPAND, 0) 
sizer_4.AddSpacer(20) 
sizer_4.Add(self.grid7, 1, wx.EXPAND, 0) 


sizer_5 = wx.BoxSizer(wx.VERTICAL) 
sizer_5.Add(self.grid2, 1, wx.EXPAND, 0) 
sizer_5.AddSpacer(20) 
sizer_5.Add(self.grid5, 1, wx.EXPAND, 0) 

sizer_6 = wx.BoxSizer(wx.VERTICAL) 
sizer_6.Add(self.grid3, 1, wx.EXPAND, 0) 
sizer_6.AddSpacer(20) 
sizer_6.Add(self.grid6, 1, wx.EXPAND, 0)  


sizer_3=wx.BoxSizer(wx.HORIZONTAL) 
sizer_3.AddSpacer(20) 
sizer_3.Add(sizer_4, 1, wx.EXPAND, 0) 
sizer_3.AddSpacer(20) 
sizer_3.Add(sizer_5, 1, wx.EXPAND, 0) 
sizer_3.AddSpacer(20) 
sizer_3.Add(sizer_6, 1, wx.EXPAND, 0) 


sizer_1= wx.BoxSizer(wx.VERTICAL) 
sizer_1.AddSpacer(10) 
sizer_1.Add(sizer_2) 
sizer_1.AddSpacer(20) 
sizer_1.Add(sizer_3, 1, wx.EXPAND, 0) 

self.SetSizer(sizer_1) 
self.Layout() 

我能做些什麼?

回答

1

我不喜歡網格sizer,因爲我覺得他們很混亂。所以我通常採取這樣的方式,圍繞各個組織畫框。這些框代表BoxSizers。一旦我完成了,我知道該怎麼做,並相應地編寫我的代碼。以下是我將如何佈置:

import wx 

######################################################################## 
class GenericPanel(wx.Panel): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, parent, color): 
     """Constructor""" 
     wx.Panel.__init__(self, parent, size=(200, 100)) 
     self.SetBackgroundColour(color) 


######################################################################## 
class MainPanel(wx.Panel): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent) 

     left_sizer = wx.BoxSizer(wx.VERTICAL) 
     for i in range(3): 
      panel = GenericPanel(self, 'red') 
      left_sizer.Add(panel, 0, wx.ALL, 5) 

     top_right_sizer = wx.BoxSizer(wx.HORIZONTAL) 
     for i in range(2): 
      panel = GenericPanel(self, 'green') 
      top_right_sizer.Add(panel, 0, wx.ALL, 5) 

     bottom_right_sizer = wx.BoxSizer(wx.HORIZONTAL) 
     for i in range(2): 
      panel = GenericPanel(self, 'blue') 
      bottom_right_sizer.Add(panel, 0, wx.ALL, 5) 

     stack_sizer = wx.BoxSizer(wx.VERTICAL) 
     stack_sizer.Add(top_right_sizer) 
     stack_sizer.Add(bottom_right_sizer) 

     main_sizer = wx.BoxSizer(wx.HORIZONTAL) 
     main_sizer.Add(left_sizer) 
     main_sizer.Add(stack_sizer) 
     main_sizer.AddStretchSpacer(prop=1) 
     self.SetSizer(main_sizer) 


######################################################################## 
class MainFrame(wx.Frame): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     """Constructor""" 
     wx.Frame.__init__(self, None, title='Boxes', size=(700,400)) 
     panel = MainPanel(self) 
     self.Show() 

if __name__ == '__main__': 
    app = wx.App(False) 
    frame = MainFrame() 
    app.MainLoop()