wxpython sizer扩展问题

时间:2016-03-08 16:41:35

标签: python wxpython

我的屏幕有6个网格,如下所示:

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 个答案:

答案 0 :(得分:1)

我不喜欢网格大小调整器,因为我觉得它们很混乱。所以我通常采取这样的方式,并围绕各组绘制框。方框代表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()