While doing a project i decided to do a mechanism in a panal of its own class, so i could manage the sizers and layout better, then i wanted to take the end result of that panel and display it in my window.

Although when i run this code as a tester, i get a very strange error message and it leaves me stumped, because its asking me for a size variable(w, h), which is vexing me to no end because im sure the sizers would have covered that.

import wx

class Panel1(wx.Panel):

    def __init(self, parent, id):
        wx.Panel.__init__(self, parent, wx.ID_ANY)

        subPanel1 = wx.Panel(SelectionMechanism, style = wx.SUNKEN_BORDER)

        sizer1 = wx.BoxSizer()
        sizer1.Add(subPanel1, proportion = 1, flag = wx.EXPAND)

class Panel2(wx.Panel):

    def __init(self, parent, id):
        wx.Panel.__init__(self, parent, wx.ID_ANY)

        subPanel2 = wx.Panel(SelectionMechanism, style = wx.SUNKEN_BORDER)

        sizer2 = wx.BoxSizer()
        sizer2.Add(subPanel2, proportion = 1, flag = wx.EXPAND)

class MyWindow(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Window', size = (500, 500))

        sizer = wx.BoxSizer()
        sizer.Add(Panel1, proportion = 1, flag = wx.EXPAND)
        sizer.Add(panel2, proportion = 1, flag = wx.EXPAND)

        MyWindow.SetSizer(sizer)

app = wx.App(redirect = False)
frame = MyWindow(parent = None, id = -1)
frame.Show()
app.MainLoop()

Can anyone help please? This code is exactly the same principle as my main project and throws the same error.

Im still tinkering with my code but i cant seem to solve the problem.

Ive gotten as far as

import wx

class Panel1(wx.Panel):

    def __init(self, parent, id):
        wx.Panel.__init__(self, parent, wx.ID_ANY)

        subPanel1 = wx.Panel(Panel1, style = wx.SUNKEN_BORDER)

        sizer1 = wx.BoxSizer()
        sizer1.Add(subPanel1, proportion = 1, flag = wx.EXPAND)
        self.SetSizer(sizer1)

class Panel2(wx.Panel):

    def __init(self, parent, id):
        wx.Panel.__init__(self, parent, wx.ID_ANY)

        subPanel2 = wx.Panel(Panel2)

        sizer2 = wx.BoxSizer()
        sizer2.Add(subPanel2, proportion = 1, flag = wx.EXPAND)
        self.SetSizer(sizer2)

class MyWindow(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Window', size = (500, 500))

        self.panel1 = Panel1(self)
        self.panel2 = Panel2(self)

        sizer = wx.BoxSizer()
        sizer.Add(self.panel1, proportion = 1, flag = wx.EXPAND)
        sizer.Add(self.panel2, proportion = 1, flag = wx.EXPAND)
        self.SetSizer(sizer)
        self.Fit()


app = wx.App(redirect = False)
frame = MyWindow(parent = None, id = -1)
frame.Show()
app.MainLoop()

So im getting close but my panels dont show. Any advice please?

Here is an example that works ...

# create a frame class containing two panel classes

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)

        self.panel1 = MyPanel1(self)
        self.panel2 = MyPanel2(self)
        
        sizer = wx.BoxSizer()
        sizer.Add(self.panel1, proportion=1, flag=wx.EXPAND)
        sizer.Add(self.panel2, proportion=1, flag=wx.EXPAND)
        self.SetSizer(sizer)


class MyPanel1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, wx.ID_ANY)
        self.SetBackgroundColour("blue")
        # more widgets here ...
        # give these panels different color so they show
        # otherwise they will match the color of self
        p1 = wx.Panel(self)
        p1.SetBackgroundColour("green")
        p2 = wx.Panel(self)
        p2.SetBackgroundColour("magenta")
        sizer = wx.BoxSizer()
        sizer.Add(p1, proportion=1, flag=wx.ALL|wx.EXPAND, border=8)
        sizer.Add(p2, proportion=1, flag=wx.ALL|wx.EXPAND, border=8)
        self.SetSizer(sizer)


class MyPanel2(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, wx.ID_ANY)
        self.SetBackgroundColour("red")


app = wx.App(0)
# create a frame, no parent, title, size
caption = "Panels in a Frame"
frame = MyFrame(None, caption, (400, 310))
frame.Show(True)
app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.