wxpython-垂直拆分水平BoxSizer

时间:2019-02-04 13:11:18

标签: python wxpython

我尝试编写一些用于用wxpython测试的小型GUI。我有一个带有两个面板的BoxSizer。但是现在我想在GUI的底部有两个面板并排放置。如何垂直拆分水平尺寸调整器?

到目前为止,这是我的代码:

import wx

class MyFrame(wx.Frame):
   def __init__(self, parent, ID, title):
       wx.Frame.__init__(self, parent, ID, title, size=(300, 250))

       panel1 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
       panel2 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)   

       panel1.SetBackgroundColour("BLUE")
       panel2.SetBackgroundColour("RED")

       box = wx.BoxSizer(wx.VERTICAL)
       box.Add(panel1, 2, wx.EXPAND)
       box.Add(panel2, 1, wx.EXPAND)    

       self.SetAutoLayout(True)
       self.SetSizer(box)
       self.Layout()    

app = wx.PySimpleApp()
frame = MyFrame(None, -1, "Sizer Test")
frame.Show()
app.MainLoop()

1 个答案:

答案 0 :(得分:1)

您需要创建一个垂直方向的BoxSizer作为顶层大小调整器,然后向其中添加水平大小调整器。这是一个示例:

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title, size=(300, 250))

        main_panel = wx.Panel(self)

        panel1 = wx.Panel(main_panel,-1, style=wx.SUNKEN_BORDER)
        panel2 = wx.Panel(main_panel,-1, style=wx.SUNKEN_BORDER)

        panel1.SetBackgroundColour("BLUE")
        panel2.SetBackgroundColour("RED")

        mainsizer = wx.BoxSizer(wx.VERTICAL)
        mainsizer.AddStretchSpacer()

        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(panel1, 2, wx.EXPAND)
        box.Add(panel2, 1, wx.EXPAND)
        mainsizer.Add(box, 1, wx.EXPAND)

        main_panel.SetSizer(mainsizer)
        self.Layout()    

app = wx.App()
frame = MyFrame(None, -1, "Sizer Test")
frame.Show()
app.MainLoop()

还请注意,wx.PySimpleApp已过时。您应该现在就使用wx.App