I did two changes, but some thing is wrong here.
“wx.BoxSizer.Replace” says detached child is destroyed only if it is not a window. In my case it should be destroyed because it’s a Sizer.
Script is attached.
Read the docs for Replace. It does a Detach and replace. Detach does not destroy the item, you will need to either destroy it yourself or call Hide on it afterwards.
I did two changes, but some thing is wrong here.
“wx.BoxSizer.Replace” says detached child is destroyed only if it is not a window. In my case it should be destroyed because it’s a Sizer.
Script is attached.
Here, lets take a little closer look at what you are doing. In OnButton you are doing a self.Replace where self is the sizer, in the call you are trying to replace ‘self’ with Grp2.
Problem 1: self is a reference to the sizer instance itself so it is not inside of itself right:
Pretend the sizer is a cabinet with some shelves in it. On each shelf in the cabinet you have a button. You can Replace a button on one shelf in the cabinet with something else. But you cant take the cabinet from one of its own shelves and Replace it with something else right, because the cabinet holds the shelves and not the other way around.
Problem 2: You then call self.Destroy which would destroy the sizer and anything you just put in it
If the sizer is a cabinet and you Replace some items in it then Destroy it, then anything inside of it that you just put there will be destroyed too.
Suggestions:
Look at my original reply to this thread, for most cases its easier to do it the way I mentioned there.
or
Restructure how you are doing this
Use subclasses of panels with the layouts in them instead of sizers that you pass a panel
Handle the Replace at the frame level since the panels will be inside the frames sizer and not
inside of their own sizer
or
Move the event handler out to the frame level
call Replace on the frames sizer not the sub sizer since the thing your replacing is inside the frames sizer and not itself.