将wxpython面板类添加到sizer

时间:2011-05-11 02:18:44

标签: python wxpython

我想将面板类添加到框架类中的sizer中,我将在代码的主线中调用它。

我得到的错误是TypeError: wx.Window, wx.Sizer, wx.Size, or (w,h) expected for item。我认为这与它是一个面板类而不是面板类的实例有关。

无论哪种方式,我怎么做这样的事情?

代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import wx

class step_1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, id=wx.ID_ANY)

        sizer = wx.BoxSizer(wx.VERTICAL)
        txtOne = wx.TextCtrl(self, wx.ID_ANY, "")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(txtOne, 0, wx.ALL, 5)

        self.SetSizer(sizer)

class step_2(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, id=wx.ID_ANY)

        sizer = wx.BoxSizer(wx.VERTICAL)
        txtOne = wx.TextCtrl(self, wx.ID_ANY, "")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(txtOne, 0, wx.ALL, 5)

        self.SetSizer(sizer)

class main_frame(wx.Frame):
    """Main Frame holding the main panel."""
    def __init__(self,*args,**kwargs):
        wx.Frame.__init__(self,*args,**kwargs)
        p = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(step_1,0,border = 5)
        sizer.Add(step_2,0,border = 5)
        p.SetSizerAndFit(sizer)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = main_frame(None,-1,size = (400,300))
    app.MainLoop()

1 个答案:

答案 0 :(得分:3)

就像你自己说的那样:你没有将step_1step_2类的实例传递给sizer,当然你应该这样做。只需创建它们的实例:

class main_frame(wx.Frame):
    """Main Frame holding the main panel."""
    def __init__(self,*args,**kwargs):
        wx.Frame.__init__(self,*args,**kwargs)
        p = wx.Panel(self)

        stp1 = step_1(p) 
        stp2 = step_2(p) 

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(stp1, 0, border = 5)
        sizer.Add(stp2, 0, border = 5)
        p.SetSizerAndFit(sizer)
        self.Show()