【问题标题】:wxpython size control with multiple panels带有多个面板的 wxpython 大小控制
【发布时间】:2013-10-17 14:55:17
【问题描述】:

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

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

问题是我不明白,因为我在使用 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()

【问题讨论】:

    标签: python wxwidgets panels sizer


    【解决方案1】:

    删除以下两行:

    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

    并删除以下行:

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

    【讨论】:

    • 我还建议设置窗扇位置,即self.main_splitter.SetSashPosition(300),以便您在打开窗口时立即看到一些东西..
    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 2013-10-18
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2015-01-08
    相关资源
    最近更新 更多