Removal of panels from sizers

I want to remove a panel from a sizer, but it doesn't get cleaned up
properly. I have a sizer that I want to dynamically add and remove panels
from:

class MyBigPanel(wxPanel):
    [...]

    def AddPanel(self, myid):
        panel = MyInsidePanel(self, -1, myid)
        self.panel_sizer.Add(panel, 0, wxEXPAND | wxALIGN_CENTRE | wxALL, 0)
        self.mypanels[myid] = panel

        self.GetSizer().Layout()
        self.Refresh()

    def RemovePanel(self, myid):
        self.panel_sizer.Remove(self.mypanels[myid])
        del self.mypanels[myid]

        self.GetSizer().Layout()
        self.Refresh()

    [...]

The panel is added correctly and all seems fine there. If I remove a panel
(via RemovePanel()) it gets cleaned up correctly, except when I try to
remove the bottom panel in the sizer (i.e. the last one). Then it isn't
cleaned up at all and all things inside the panel are drawn but doesn't
follow the sizer rules anymore. I use python 2.3.3 and wxPython 2.4.2.4
for Gtk+. Is this a known wxPython bug? What can I do to avoid this
behaviour?

/Ragnar

Ragnar Ouchterlony wrote:

I want to remove a panel from a sizer, but it doesn't get cleaned up
properly. I have a sizer that I want to dynamically add and remove panels
from:

class MyBigPanel(wxPanel):
    [...]

    def AddPanel(self, myid):
        panel = MyInsidePanel(self, -1, myid)
        self.panel_sizer.Add(panel, 0, wxEXPAND | wxALIGN_CENTRE | wxALL, 0)
        self.mypanels[myid] = panel

        self.GetSizer().Layout()
        self.Refresh()

    def RemovePanel(self, myid):
        self.panel_sizer.Remove(self.mypanels[myid])
        del self.mypanels[myid]

Deleting the Python object does not actually delete the C++ window object because it is not owned by the Python object. You need to call self.mypanels[myid].Destroy() first.

···

        self.GetSizer().Layout()
        self.Refresh()

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!