Replace widget at run time

A panel and three button on a vertical sizer. (Just for the an example)
p = wx.Panel(self, wx.ID_ANY)
b1 = wx.Button(p, wx.ID_ANY)
b2 = wx.Button(p, wx.ID_ANY)
b3 = wx.Button(p, wx.ID_ANY)

Now how do I replace button(b2) at run time with any other widget? For example wx.StaticText.

Thanks

Prashant Saxena wrote:

A panel and three button on a vertical sizer. (Just for the an example)
p = wx.Panel(self, wx.ID_ANY)
b1 = wx.Button(p, wx.ID_ANY)
b2 = wx.Button(p, wx.ID_ANY)
b3 = wx.Button(p, wx.ID_ANY)

Now how do I replace button(b2) at run time with any other widget? For example wx.StaticText.

Thanks

The sizer's have methods for this. I think you want sizer.Remove(widget) or sizer.Detach(widget) to get rid of the widget. Then to put the other one in, you would use the Add() method of your sizer. Something like this should add it back in (I think): sizer.Add(newWidget, pos=2).

See the docs:

http://www.wxpython.org/docs/api/wx.Sizer-class.html

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Hello,

Prashant Saxena wrote:

A panel and three button on a vertical sizer. (Just for the an example)
p = wx.Panel(self, wx.ID_ANY)

b1 = wx.Button(p, wx.ID_ANY)
b2 = wx.Button(p, wx.ID_ANY)
b3 = wx.Button(p, wx.ID_ANY)

Now how do I replace button(b2) at run time with any other widget? For example wx.StaticText.

Thanks

The sizer’s have methods for this. I think you want sizer.Remove(widget) or sizer.Detach(widget) to get rid of the widget. Then to put the other one in, you would use the Add() method of your sizer. Something like this should add it back in (I think): sizer.Add(newWidget, pos=2).

See the docs:

http://www.wxpython.org/docs/api/wx.Sizer-class.html
Typically if I know what needs to be swapped at a certain time I will during the intial layout create both controls and put them in the sizer next to eachother.

i.e)

vsizer = wx.BoxSizer(wx.VERTICAL)

p = wx.Panel(self, wx.ID_ANY)
b1 = wx.Button(p, wx.ID_ANY)
b2 = wx.Button(p, wx.ID_ANY)

s1 = wx.StaticText(p, label=“MyText”)
b3 = wx.Button(p, wx.ID_ANY)

vsizer.AddMany([…])

Then Hide the one that I don’t want to show at first

s1.Show(False)

Then in your handler for switching them just simply call

b2.Show(False)

s1.Show(True)

sizer.Layout()

I find this far simpler than trying to deal with finding the item in the sizer and using Remove to remove it then figuring out the index to use in the Insert call for the new item.

Cody

···

On Tue, Sep 9, 2008 at 12:21 PM, Mike Driscoll mike@pythonlibrary.org wrote:

Mike Driscoll wrote:

Prashant Saxena wrote:

A panel and three button on a vertical sizer. (Just for the an example)
p = wx.Panel(self, wx.ID_ANY)
b1 = wx.Button(p, wx.ID_ANY)
b2 = wx.Button(p, wx.ID_ANY)
b3 = wx.Button(p, wx.ID_ANY)

Now how do I replace button(b2) at run time with any other widget? For example wx.StaticText.

Thanks

The sizer's have methods for this. I think you want sizer.Remove(widget) or sizer.Detach(widget) to get rid of the widget.

No need, just call the widget's Destroy method and it will remove itself from the sizer first. OTOH, I usually use Cody's approach and just di a Hide/Show with a Layout to switch between the states.

Then to put the other one in, you would use the Add() method of your sizer. Something like this should add it back in (I think): sizer.Add(newWidget, pos=2).

It's sizer.Insert to do something other than appending to the end.

···

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

Robin Dunn wrote:

<div class="moz-text-flowed" style="font-family: -moz-fixed">Mike Driscoll wrote:

Prashant Saxena wrote:

A panel and three button on a vertical sizer. (Just for the an example)
p = wx.Panel(self, wx.ID_ANY)
b1 = wx.Button(p, wx.ID_ANY)
b2 = wx.Button(p, wx.ID_ANY)
b3 = wx.Button(p, wx.ID_ANY)

Now how do I replace button(b2) at run time with any other widget? For example wx.StaticText.

Thanks

The sizer's have methods for this. I think you want sizer.Remove(widget) or sizer.Detach(widget) to get rid of the widget.

No need, just call the widget's Destroy method and it will remove itself from the sizer first. OTOH, I usually use Cody's approach and just di a Hide/Show with a Layout to switch between the states.

Since you and Cody are saying this, I think I would be wise to follow your examples... :slight_smile:

Then to put the other one in, you would use the Add() method of your sizer. Something like this should add it back in (I think): sizer.Add(newWidget, pos=2).

It's sizer.Insert to do something other than appending to the end.

Crumb! I must have some wacky code then as I was using Add and it worked...of course, I was adding to a grid widget and I think you can specify the position of the widget being added. I should have mentioned Insert too though.

Mike

Mike Driscoll wrote:

Robin Dunn wrote:

It's sizer.Insert to do something other than appending to the end.

Crumb! I must have some wacky code then as I was using Add and it worked...of course, I was adding to a grid widget and I think you can specify the position of the widget being added.

Yes, with the GridBagSizer you can specify the cell position in the Add, so Add would be fine with that sizer. All the others use the position in the child list to determine layout position, so adding to the end of that list wont work unless you really want it to be in the last position, therefore Insert and Prepend methods are provided.

···

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