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.
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).
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.
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.
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!
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...
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.
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!