[wxPython] wxBoxSizer - how to use it?

Greetings,

Can someone please explain me the right way to use sizers in the
following situation:

I want to have a Dialog (or Frame, that doesn't matter) with layout as
in wxBoxSizer example in the wxWindows doc

http://www.lpthe.jussieu.fr/~zeitlin/wxWindows/docs/wxwin475.htm#boxsize
rprogramming

except that OK button should be aligned to the left and Cancel button
should be aligned to the right.

To achieve this I inserted a spacer between this two buttons.

I've modified wxWindows doc's example in following way (see attached
script) and I am facing very strange behaviour.

When I first start my program, I see OK button aligned to the left-top
instead of left-center and see no Cancel button. After *any* resize,
Cancel button appears and buttons layouted in the way I want.

What is the reason for this strange behaviour? What is the correct way
to implement the layout I want?

I also think it would be a good improvement to have such example in the
demo program.

Sincerely Yours, Victor.

I use wxWindows 2.3.3.1, python 2.2.2, Windows 2000 if this can help.

=======>
from wxPython.wx import *

class MyDialog(wxDialog):
    def __init__(self, parent, id, title):
        wxDialog.__init__(self, parent, id, title,
                          style = wxDEFAULT_DIALOG_STYLE |
wxRESIZE_BORDER)

        topsizer = wxBoxSizer(wxVERTICAL)
        topsizer.Add(
            wxTextCtrl(self, -1, "My text.", pos = wxDefaultPosition,
                       size = wxSize(100, 60), style = wxTE_MULTILINE),
            1, wxEXPAND | wxALL, 10)

        button_panel = wxPanel(self, -1)
        button_sizer = wxBoxSizer(wxHORIZONTAL)
        button_sizer.Add(wxButton(button_panel, wxID_OK, "OK"), 0,
wxALL, 10)
        button_sizer.Add(0,0,1)
        button_sizer.Add(wxButton(button_panel, wxID_CANCEL, "Cancel"),
0, wxALL, 10)

        button_sizer.Fit(button_panel)
        button_panel.SetAutoLayout(true)
        button_panel.SetSizer(button_sizer)

        topsizer.Add(button_panel, 0, wxALIGN_CENTER | wxEXPAND)

        self.SetSizer(topsizer)
        topsizer.SetSizeHints(self)

class MyApp(wxApp):
    def OnInit(self):
        dialog = MyDialog(None, -1, "My dialog.")
        self.SetTopWindow(dialog)
        dialog.ShowModal()
        dialog.Destroy()

        return true

if __name__ == '__main__':
    app = MyApp(0)
    app.MainLoop()
<=======

Krjukov Victor wrote:

When I first start my program, I see OK button aligned to the left-top
instead of left-center and see no Cancel button. After *any* resize,
Cancel button appears and buttons layouted in the way I want.

What is the reason for this strange behaviour? What is the correct way
to implement the layout I want?

If the frame or dialog has already been sized to its final size by the time the sizer size setup then you'll see this behaviour because the Layout methods are normally called from a EVT_SIZE event handler. As you discovered all you have to do is call Layout yourself.

···

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