框架和面板中的BoxSizer

时间:2014-03-19 18:36:53

标签: python wxpython 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

然后出现问题:所有元素都位于左上角,它们相互重叠。当我想在我的班级 panelThree 中使用BoxSizer时,如何使用BoxSizer? 我刚刚从这里拿到了例子:http://wiki.wxpython.org/BoxSizerTutorial

1 个答案:

答案 0 :(得分: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()