[wxPython] setsizer

Hi,

I have a small wxpython program (see code below) that did not behave as I
expected. I am using wxwindows 2.3.3pre5.

__init__ in class MyFrame ends with twice the line
'panel.SetSizer(GetSizer1(panel))'.

I expected that the second line would make no difference, because in the
docs for wxwindow.setsizer I found that any prexisting sizer will be
deleted. So I thought that by executing the second line, the sizer would
simply be replaced by another sizer (with the same children).

This is not what happens, an extra wxTextCtrl is added in the left upper
corner.

So I would appreciate it if somebody could tell me what I am missing, and
how I can change the contents of a panel by giving it another sizer.

Thanks in advance.

from wxPython.wx import *

ID_FIELD1 = 20001
ID_TEXT1 = 20002

def GetSizer1(parent):
    item0 = wxBoxSizer( wxVERTICAL )
    item1 = wxFlexGridSizer( 0, 2, 0, 0 )
    item2 = wxStaticText( parent, ID_FIELD1, "Field1", wxDefaultPosition,
wxDefaultSize, 0 )
    item1.AddWindow( item2, 0, wxALIGN_CENTRE|wxALL, 5 )
    item7 = wxTextCtrl( parent, ID_TEXT1, "", wxDefaultPosition,
wxSize(80,-1), 0 )
    item1.AddWindow( item7, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 )
    item0.AddSizer( item1, 0, wxALIGN_CENTRE|wxALL, 5 )
    return item0

class MyFrame(wxFrame):
    def __init__(self, parent, id, title,
        pos = wxPyDefaultPosition, size = wxPyDefaultSize,
        style = wxDEFAULT_FRAME_STYLE ):
        wxFrame.__init__(self, parent, id, title, pos, size, style)
        EVT_CLOSE(self, self.OnCloseWindow)
        panel = wxPanel(self, -1)
        panel.SetAutoLayout( true )
        panel.SetSizer(GetSizer1(panel))
        panel.SetSizer(GetSizer1(panel))

    def OnCloseWindow(self, event):
        self.Destroy()

class MyApp(wxApp):

    def OnInit(self):
        wxInitAllImageHandlers()
        frame = MyFrame(None, -1, "Test", wxPoint(50,50), wxSize(400,300) )
        frame.Show(true)
        return true

app = MyApp(1)
app.MainLoop()

I expected that the second line would make no difference, because in the
docs for wxwindow.setsizer I found that any prexisting sizer will be
deleted. So I thought that by executing the second line, the sizer would
simply be replaced by another sizer (with the same children).

The existing sizer is deleted, but it does not delete the windows in the
sizer because they do not belong it it, they are childred of the window. If
you want to change the contents of the panel then you need to Destroy() all
the children and then create new ones and then put them in the new sizer.

···

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

As an alternative, you might create your own NewSizer class that inherits
from wxStaticBoxSizer (or whichever sizer you are using). You can add member
variables for parentWindow and children and override the AddWindow() method
to update the children list before or after calling wxBoxSizer.AddWindow.
I've used this technique to implement a sizer.Enable() method to
enable/disable the wxStaticBox as well as all "children" of NewSizer.

···

-----Original Message-----
From: wxpython-users-admin@lists.wxwindows.org
[mailto:wxpython-users-admin@lists.wxwindows.org]On Behalf Of Robin Dunn
Sent: Wednesday, August 28, 2002 1:10 PM
To: wxpython-users@lists.wxwindows.org
Subject: Re: [wxPython] setsizer

>
> I expected that the second line would make no difference, because in the
> docs for wxwindow.setsizer I found that any prexisting sizer will be
> deleted. So I thought that by executing the second line, the sizer would
> simply be replaced by another sizer (with the same children).

The existing sizer is deleted, but it does not delete the windows in the
sizer because they do not belong it it, they are childred of the
window. If
you want to change the contents of the panel then you need to
Destroy() all
the children and then create new ones and then put them in the new sizer.

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

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