[wxPython-users] Changing content of a sizer

Mike Rooney mxr at qvii.com
Thu Aug 2 10:06:28 PDT 2007


Norberto Lopes wrote:
> Thanks for the quick answer.
>
> At Thu, 02 Aug 2007 18:21:35 +0200,
> "Werner F. Bruhin" <werner.bruhin at free.fr> wrote:
>   =

>> Norberto Lopes wrote:
>>     =

>>> Can someone give me some insight as to how to change the contents of a
>>> Sizer?
>>> I have a pane, which has two sizers. I want to change the content of
>>> one of its sizers dinamically.
>>>   =

>>>       =

>> Have a look at the gridbagsizer demo, it does hide/show of items.
>>     =

> In gridbagsizerdemo, they just hide things.
> I don't want that. I want to actually change the contents of a sizer
> for something else. Or maybe just change one sizer for another (this
> solution I think may be easier).
>
>   =



How about this little demo I created? Basically all you need to do is =

use the Replace method of a sizer, Destroy the old window, and re-Layout.

Personally, I typically just use the Hide/Show mechanism as my uses are =

generally cases where the controls are not one-time use so it saves time =

because they don't have to be constantly re-created. I have never tried =

the "replace" method before but thought I would give it a go. If anyone =

knows of any issues with this, please let me know!

- Mike
-------------- next part --------------
import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        =

        buttonFirst =3D wx.Button(self, label=3D"First")
        =

        self.button =3D button =3D wx.Button(self, label=3D"Click to replac=
e me!")
        button.Bind(wx.EVT_BUTTON, self.OnReplace)
        =

        buttonLast =3D wx.Button(self, label=3D"Last")
        =

        self.Sizer =3D wx.BoxSizer(wx.VERTICAL)
        self.Sizer.Add(buttonFirst)
        self.Sizer.Add(button)
        self.Sizer.Add(buttonLast)
        =

    def OnReplace(self, event):
        #create the replacement
        replacement =3D wx.StaticText(self, label=3D"Replaced!")
        #replace it in the sizer
        self.Sizer.Replace(self.button, replacement)
        #destroy the old control
        self.button.Destroy()
        #re-layout
        self.Sizer.Layout()
        =

        =

if __name__ =3D=3D "__main__":
    app =3D wx.App(False)
    MyFrame().Show(True)
    app.MainLoop()


More information about the wxpython-users mailing list