Replace widget at run time

Well, I gave a try to one of your suggestions by creating a custom Panel and do the replacement in there.
Still out of luck.
script attached.

replace.zip (657 Bytes)

···

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

Hello,

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

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

Hello,

Please read my previous message more carefully. You are still doing almost the same same thing as before. You must have also overlooked my first suggestion as well. I usually don’t write code for people but here is a sample of my first suggestion, which IMHO is much simplier than replacing things in the sizer for most use cases.

The second suggestion was to Not use subclasses of sizers but have two panel classes one containing your buttons and one containing your spinners. Then in the main panel the one in your frame, take its sizer and do the swapping there because it is the container for the controls.

Also for future reference for small single sample files like this zipping it up only makes it more work for other people to look at, simply attach the file.

Cody

replace.py (1.75 KB)

···

On Wed, Sep 10, 2008 at 2:33 PM, Prashant Saxena animator333@yahoo.com wrote:

Well, I gave a try to one of your suggestions by creating a custom Panel and do the replacement in there.
Still out of luck.
script attached.

----- Original Message ----
From: Cody Precord codyprecord@gmail.com
To: wxpython-users@lists.wxwidgets.org

Sent: Wednesday, September 10, 2008 10:12:54 PM
Subject: Re: [wxpython-users] Replace widget at run time

Hello,

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

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
  1. Use subclasses of panels with the layouts in them instead of sizers that you pass a panel
  1. 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
  1. call Replace on the frames sizer not the sub sizer since the thing your replacing is inside the frames sizer and not itself.
  1. Destroy the old item

cody


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org

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