Small square on upper left corner of window when panel is switched

I am facing a problem with wx.panels.
In my project have created file with wx.frame inherited in a class in a file and other two files have wx.panels inherited.
I am trying to hide/destroy panel1 and call panel2 but somehow its not working. A small square icon with panel background color appears on switch.
I refered this topic on stackoverflow but I found if I do not use sizers the code does not work (when split in different files.)

the code that I refered:

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        self.panel = wx.Panel(self)
        self.btn = wx.Button(self.panel, label="Panel 1", size=(250,75))
        self.btn.Bind(wx.EVT_BUTTON, self.switch)

        vbox1 = wx.BoxSizer(wx.VERTICAL)
        vbox1.Add(self.btn)
        self.panel.SetSizer(vbox1)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.panel)
        self.SetSizer(vbox)

        self.Show()

    def switch(self, event):
        self.parent.Swap()

class MyOtherPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        self.panel = wx.Panel(self)
        self.btn = wx.Button(self.panel, label="Panel 2", size=(175,250))
        self.btn.Bind(wx.EVT_BUTTON, self.switch)

        vbox1 = wx.BoxSizer(wx.VERTICAL)
        vbox1.Add(self.btn)
        self.panel.SetSizer(vbox1)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.panel)
        self.SetSizer(vbox)

        self.Show()
        self.panel.Hide()

    def switch(self, event):
        self.parent.Swap()

class PanelSwitcher(wx.Frame):
    def __init__(self):
        super().__init__(None)

        vbox = wx.BoxSizer(wx.VERTICAL)
        self.panel1 = MyPanel(self)
        self.panel2 = MyOtherPanel(self)
        vbox.Add(self.panel1)
        vbox.Add(self.panel2)
        self.SetSizer(vbox)
        self.Show()

    def Swap(self):
        if self.panel1.panel.IsShown():
            self.panel1.panel.Hide()
            self.panel2.panel.Show()
        else:
            self.panel2.panel.Hide()
            self.panel1.panel.Show()
        self.Layout()


if __name__ == "__main__":
    app = wx.App()
    PanelSwitcher()
    app.MainLoop()

can anyone suggest what am I missing?
Thanks

Well, I haven’t been through your coding, but a panel to me is just a sort of helper class for some basic functionality for a window and for what you try to achieve I don’t think that class would have crossed my mind
But I have read, for me astonishing, ‘panel posts’ on here before and I’m very interested on deeper insights :upside_down_face:

When you subclass wx.Panel you have a panel - you do not need to create another panel as in:
self.panel = wx.Panel(self) - unless you need a panel within a panel.
Therefore “self” is your panel. You then can create the sizer and add the button to the sizer , then add the sizer to the panel (in this case it is panel =“self”). Remove the extra panel stuff and the extra sizer. Most everything else looks ok. BTW it would be nice if you provided platform, python version, wxpython version.

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        #self.panel = wx.Panel(self)
        self.btn = wx.Button(self, label="Panel 1", size=(250,75))
        self.btn.Bind(wx.EVT_BUTTON, self.switch)

        vbox1 = wx.BoxSizer(wx.VERTICAL)
        vbox1.Add(self.btn)
        #self.panel.SetSizer(vbox1)
        self.SetSizer(vbox1)

        #vbox = wx.BoxSizer(wx.VERTICAL)
        #vbox.Add(self.panel)
        #self.SetSizer(vbox)

        self.Show()

    def switch(self, event):
        self.parent.Swap()

class MyOtherPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        #self.panel = wx.Panel(self)
        self.btn = wx.Button(self, label="Panel 2", size=(175,250))
        self.btn.Bind(wx.EVT_BUTTON, self.switch)

        vbox1 = wx.BoxSizer(wx.VERTICAL)
        vbox1.Add(self.btn)
        self.SetSizer(vbox1)

        #vbox = wx.BoxSizer(wx.VERTICAL)
        #vbox.Add(self.panel)
        #self.SetSizer(vbox)

        self.Show()
        self.Hide()

    def switch(self, event):
        self.parent.Swap()

class PanelSwitcher(wx.Frame):
    def __init__(self):
        super().__init__(None)

        vbox = wx.BoxSizer(wx.VERTICAL)
        self.panel1 = MyPanel(self)
        self.panel2 = MyOtherPanel(self)
        vbox.Add(self.panel1)
        vbox.Add(self.panel2)       
        self.SetSizer(vbox)
        self.Show()

    def Swap(self):
        if self.panel1.IsShown():
            self.panel1.Hide()
            self.panel2.Show()
        else:
            self.panel2.Hide()
            self.panel1.Show()
        self.Layout()
        self.Refresh()


if __name__ == "__main__":
    app = wx.App()
    PanelSwitcher()
    app.MainLoop()

In addition to the comments about not needing an extra panel on top of a panel, you are also not adding items to the frame’s sizer in a way that will cause the items to expand to fill the client area of the frame. For example, try this change:

        vbox.Add(self.panel1, 1, wx.EXPAND)
        vbox.Add(self.panel2, 1, wx.EXPAND)

Also, a great way to interactively explore and help diagnose layout issues is to use the Widget Inspection Tool.

You may also want to take a look at the wx.Simplebook class. It is like a notebook without tabs, and is an easy way to use a set of panels where only one is visible at a time.