2014-03-19 111 views
0

當我創建一個BoxSizer這樣的:BoxSizer在框架和麪板

class MyForm(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, 
         "App",size=(800,600),style= wx.SYSTEM_MENU | wx.CAPTION | wx.MINIMIZE_BOX | wx.CLOSE_BOX) 
     self.panel=wx.Panel(self,size=(800,600)) 
     # create BoxSizer and fill it with elements 

它的工作原理。 但是當我這樣做:

class MyForm(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, 
         "App",size=(800,600),style= wx.SYSTEM_MENU | wx.CAPTION | wx.MINIMIZE_BOX | wx.CLOSE_BOX) 
     panelThree(self) 

class panelThree(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent=parent,size=(800,600)) 
     self.panel=wx.Panel(self,size=(800,600)) 
     # create BoxSizer and fill it with elements 

那麼不順心的事:所有元素都位於左上角,他們相互重疊。當我想在我的課堂上使用BoxSizer時,如何使用BoxSizer panelThree? 我剛剛從這裏舉了個例子:http://wiki.wxpython.org/BoxSizerTutorial

回答

0

問題很可能是您沒有將以下小部件添加到sizer中:self.panel。如果你不把它添加到sizer中,那麼它會弄亂你的佈局。就我個人而言,我認爲你甚至不需要面板內的面板。刪除該行將解決問題:

import wx 

class MyForm(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, 
         "App",size=(800,600),style= wx.SYSTEM_MENU | wx.CAPTION | wx.MINIMIZE_BOX | wx.CLOSE_BOX) 
     panelThree(self) 

class panelThree(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent=parent,size=(800,600)) 
     self.panel=wx.Panel(self,size=(800,600)) 
     # create BoxSizer and fill it with elements 
     sizer = wx.BoxSizer(wx.VERTICAL) 

     for item in range(10): 
      btn = wx.Button(self, label="Button %s" % item) 
      sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5) 

     self.SetSizer(sizer) 

#---------------------------------------------------------------------- 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyForm() 
    frame.Show() 
    app.MainLoop()