Search code examples
pythonwxpython

Adding wxpython panel classes to sizers


I want to add panel classes to a sizer in a frame class that I will call in the main line of the code.

The error I get is TypeError: wx.Window, wx.Sizer, wx.Size, or (w,h) expected for item. I assume this has to do with the fact that it is a panel class and not the instance of the panel class..

Either way, how would I do such a thing?

Code is below:

#!/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()

Solution

  • Like you said yourself: You're not passing instances of the step_1 and step_2 classes to the sizer, which of course you should be. Simply create instances of them:

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