Replace widget at run time

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.

replace.zip (640 Bytes)

···

----- Original Message ----
From: Cody Precord codyprecord@gmail.com
To: wxpython-users@lists.wxwidgets.org
Sent: Wednesday, September 10, 2008 6:44:09 PM
Subject: Re: [wxpython-users] Replace widget at run time

Hello,

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.

Cody

On Wed, Sep 10, 2008 at 7:22 AM, Prashant Saxena animator333@yahoo.com wrote:

I did a simple experiment for replacing widgets at run time. Script is attached.
Problem is the older one is not getting deleted.

Thanks


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Hello,

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:

  1. Look at my original reply to this thread, for most cases its easier to do it the way I mentioned there.

or

  1. Restructure how you are doing this

  2. Use subclasses of panels with the layouts in them instead of sizers that you pass a panel

  3. Handle the Replace at the frame level since the panels will be inside the frames sizer and not

inside of their own sizer

or

  1. Move the event handler out to the frame level

  2. call Replace on the frames sizer not the sub sizer since the thing your replacing is inside the frames sizer and not itself.

  3. Destroy the old item

cody

···

On Wed, Sep 10, 2008 at 9:47 AM, Prashant Saxena animator333@yahoo.com wrote: