具有多个面板的wxpython大小控件

时间:2013-10-17 14:55:17

标签: python wxwidgets panels sizer

我正在尝试展示一个包含三个面板的全屏应用。屏幕应首先分成两个垂直面板,右边一个分为两个水平面板。类似的东西:

____________________
|        |         |
|        |         |
|        |_________|
|        |         |
|        |         |
|________|_________|

问题在于我没有这样做,因为我对sizer有一些问题。这是我的尝试:

import wx

class Input_Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # Input variables
        self.tittle1 = wx.StaticText(self, label="Inputs:")    
        self.lblname1 = wx.StaticText(self, label="Input 1:")
        self.format1 = ['Option 1','Option 2']
        self.combo1 = wx.ComboBox(self, size=(200, -1),value='', choices=self.format1,style=wx.CB_DROPDOWN)
        self.lblname2 = wx.StaticText(self, label="Input 2")
        self.format2 = ['Option 1','Option 2', 'Option 3']
        self.combo2 = wx.ComboBox(self, size=(200, -1),value='', choices=self.format2, style=wx.CB_DROPDOWN)

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(2, 2)
        self.sizer.Add(self.tittle1, (1, 2))
        self.sizer.Add(self.lblname1, (2, 1))
        self.sizer.Add(self.combo1, (2, 2))
        self.sizer.Add(self.lblname2, (3, 1))
        self.sizer.Add(self.combo2, (3, 2))
        self.SetSizer(self.sizer)

class Output_Panel1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # Output variables
        self.tittle2 = wx.StaticText(self, label="Outputs:")    
        self.lblname3 = wx.StaticText(self, label="Output1")
        self.result3 = wx.StaticText(self, label="", size=(100, -1))

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(2, 2)
        self.sizer.Add(self.tittle2, (1, 2))
        self.sizer.Add(self.lblname3, (2, 1))
        self.sizer.Add(self.result3, (2, 2))
        self.SetSizer(self.sizer)

class Output_Panel2(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # Output variables
        self.tittle2 = wx.StaticText(self, label="Outputs:")    
        self.lblname3 = wx.StaticText(self, label="Output1")
        self.result3 = wx.StaticText(self, label="", size=(100, -1))

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(2, 2)
        self.sizer.Add(self.tittle2, (1, 2))
        self.sizer.Add(self.lblname3, (2, 1))
        self.sizer.Add(self.result3, (2, 2))
        self.SetSizer(self.sizer)



class Main_Window(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title, pos = (0, 0), size = wx.DisplaySize())

        # Set variable panels
        self.main_splitter = wx.SplitterWindow(self)
        self.out_splitter = wx.SplitterWindow(self.main_splitter)
        self.inputpanel = Input_Panel(self.main_splitter)
        self.inputpanel.SetBackgroundColour('#c4c4ff')
        self.outputpanel1 = Output_Panel1(self.out_splitter)
        self.outputpanel1.SetBackgroundColour('#c2f1f5')
        self.outputpanel2 = Output_Panel2(self.out_splitter)
        self.outputpanel2.SetBackgroundColour('#c2f1f5')
        self.main_splitter.SplitVertically(self.inputpanel, self.main_splitter)
        self.main_splitter.SplitHorizontally(self.outputpanel1, self.outputpanel2)

        # Set sizers
        self.windowSizer1 = wx.BoxSizer(wx.VERTICAL)
        self.windowSizer2 = wx.BoxSizer(wx.HORIZONTAL)
        self.windowSizer2.Add(self.out_splitter, 1, wx.ALL | wx.EXPAND)
        self.windowSizer1.Add(self.windowSizer2, 1, wx.ALL | wx.EXPAND)    
        self.SetSizer(self.windowSizer1)

def main():
    app = wx.App(False)
    frame = Main_Window(None, "App GUI")
    frame.Show()
    app.MainLoop()

if __name__ == "__main__" :
    main()

1 个答案:

答案 0 :(得分:3)

删除以下两行:

self.main_splitter.SplitVertically(self.inputpanel, self.main_splitter)
self.main_splitter.SplitHorizontally(self.outputpanel1, self.outputpanel2)

使用:

self.main_splitter.SplitVertically(self.inputpanel, self.out_splitter)
self.out_splitter.SplitHorizontally(self.outputpanel1, self.outputpanel2)

这些行有什么问题

  1. 在第一行中,代码正在将main_splitter传递给main_splitter.SplitVertically
  2. 第二行再次拆分main_splitter

  3. 删除以下行:

    # Set sizers
    self.windowSizer1 = wx.BoxSizer(wx.VERTICAL)
    self.windowSizer2 = wx.BoxSizer(wx.HORIZONTAL)
    self.windowSizer2.Add(self.out_splitter, 1, wx.ALL | wx.EXPAND)
    self.windowSizer1.Add(self.windowSizer2, 1, wx.ALL | wx.EXPAND)    
    self.SetSizer(self.windowSizer1)