2013-10-10 43 views
0

當我應用以下代碼時,第二個面板顯示在第一個面板上。我想知道如何讓兩個面板沒有重疊並且具有合適的窗口大小(框架)。我也想知道是否有任何合適的方式在面板中繪製某種標題(在這種情況下,「輸入:」和「輸出:」)。謝謝!wxpython面板大小和標題

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_Panel(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) 

     # Set variable panels 
     self.splitter = wx.SplitterWindow(self) 
     self.panel1 = Input_Panel(self.splitter) 
     self.panel2 = Output_Panel(self.splitter) 
     self.splitter.SplitVertically(self.panel1, self.panel2) 

     self.windowSizer = wx.BoxSizer(wx.VERTICAL) 
     self.windowSizer.Add(self.splitter, 1, wx.ALL | wx.EXPAND) 
     self.SetSizerAndFit(self.windowSizer) 

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

if __name__ == "__main__" : 
    main() 

回答

0

修改

self.SetSizer(self.sizer) 

到Input_Panel和output_Panel類還可以正確調整面板大小。

0

您可以使用Widget檢測工具來幫助您弄清楚發生了什麼。我用它的突出功能來確定每個面板的位置和尺寸。你可以在這裏閱讀更多關於它的信息:http://wiki.wxpython.org/Widget%20Inspection%20Tool

SplitterWindow不能平分項目。您需要調用分離器的SetMinimumPaneSize以使它們都可見。我修改了代碼,告訴你什麼是我說的,也演示瞭如何添加檢測工具:通過

self.SetSizerAndFit(self.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_Panel(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, size=(800,600)) 

     # Set variable panels 
     self.splitter = wx.SplitterWindow(self, style = wx.SP_LIVE_UPDATE) 
     self.panel1 = Input_Panel(self.splitter) 
     self.panel2 = Output_Panel(self.splitter) 
     self.splitter.SplitVertically(self.panel1, self.panel2) 
     w, h = self.GetSize() 
     self.splitter.SetMinimumPaneSize(w/2) 

     self.windowSizer = wx.BoxSizer(wx.VERTICAL) 
     self.windowSizer.Add(self.splitter, 1, wx.ALL | wx.EXPAND) 

     #self.SetSizerAndFit(self.windowSizer) 

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

if __name__ == "__main__" : 
    main() 
+0

非常感謝,這很有效,但我想知道是否可以爲每個面板設置不同的尺寸。在擴展代碼中,右側面板比左側面板小得多,使用「self.splitter.SetMinimumPaneSize(120)」時看起來不太好。我可以添加一個標題嗎? – wuampa

+0

我實際上會使用框架的大小,然後將窗格大小設置爲它們的一半以使它們相等。至於標題,你將不得不使用StaticText小部件或類似的 –

+0

我知道我想要太多,但我可以問你一個例子嗎? – wuampa