"Tkinter"-like layout with sizers and frames ?

From: Hans Nowak [mailto:zephyr01@alltel.net]
Sent: Tuesday, April 29, 2003 12:34 AM
To: wxpython-users@lists.wxwindows.org
Subject: [wxPython-users] "Tkinter"-like layout with sizers
and frames?

Howdy y'all,

I am trying to emulate a window layout not unlike Tkinter's
packing mechanism.
I am using Sizers for this, but keep on running into
problems, so I'm
wondering if what I want is possible at all. Here's a short
description:

Let's say we have a frame f1 with three buttons A, B and C,
in a horizontal
row. There's also a frame f2 with three buttons 1, 2 and 3,
tiled vertically.
f1's buttons have f1 as parent, and f2's have f2 as parent.
What I intend to
do is make f2 the main frame, and add f1 to it as a subframe.
So, the final
window would roughly look like this:

I think you need to have one "main" frame, a window derived from wxFrame and
a bunch of
wxPanels that are children of the "main" frame. I think the code below will
do what you want..

from wxPython.wx import *

class MyFrame( wxFrame ):
    def __init__( self ):
        wxFrame.__init__( self, None, -1, "Test" )

        # The first panel contains three horizontal buttons
        pnl = wxPanel( self, -1 )
        pnl.SetBackgroundColour( wxRED )
        szr = wxBoxSizer( wxHORIZONTAL )
        btn1 = wxButton( pnl, 1, "Button 1", wxDefaultPosition )
        btn2 = wxButton( pnl, 2, "Button 2", wxDefaultPosition )
        btn3 = wxButton( pnl, 3, "Button 3", wxDefaultPosition )
        szr.Add( btn1, 0, wxCENTER )
        szr.Add( btn2, 0, wxCENTER )
        szr.Add( btn3, 0, wxCENTER )
        pnl.SetSizer( szr )
        pnl.SetAutoLayout( true )

        # Second panel contains three vertical buttons
        pnl2 = wxPanel( self, -1 )
        szr = wxBoxSizer( wxVERTICAL )
        btn1 = wxButton( pnl2, 1, "Button 1", wxDefaultPosition )
        btn2 = wxButton( pnl2, 2, "Button 2", wxDefaultPosition )
        btn3 = wxButton( pnl2, 3, "Button 3", wxDefaultPosition )
        szr.Add( btn1, 0, wxCENTER )
        szr.Add( btn2, 0, wxCENTER )
        szr.Add( btn3, 0, wxCENTER )
        pnl2.SetSizer( szr )
        pnl2.SetAutoLayout( true )

        # Now create a sizer for the main frame and add the panels to it
        szr = wxBoxSizer( wxVERTICAL )
        szr.Add( pnl, 1, wxGROW )
        szr.Add( pnl2, 1, wxGROW )
        self.SetSizer( szr )
        self.SetSize( wxSize( 800, 600 ) )
        self.SetAutoLayout( true )
        self.Layout()
    
class MyApp( wxApp ):
    def OnInit( self ):
        frame = MyFrame()
        frame.Show( true )
        return true

def main():
    app = MyApp(0)
    app.MainLoop()

if __name__ == "__main__":
    main()

button 1 (long)
button 2 (long)
button 3 (long)
buttons A, B and C in a row

I can easily do this if I use sizers properly. That is, 1, 2
and 3 go in one
sizer, A, B and C in another, one sizer is added to the
other, and all buttons
have the same frame as their parent. This is how the
examples in the demo seem
to do it.

However, I would like to do this in the way described above,
with two frames
that are both parents to the buttons they contain, and they
both have sizers.
Is that possible? As said, I would like to emulate the
Tkinter way of doing
this, with frames added to frames.

Sorry if this is unclear. I'd post example code, but
everything I tried so far
yields the wrong layout and/or crashes the interpreter.

Thanks,

The only trick to doing it the way I did it above, is that you have to add
the panels to the main frame's sizers. Which may be confusing to have
nested sizers like that. But, if you think in terms of each panel being
autonomous, in regards to layout, it really isn't too bad.

jw

ยทยทยท

-----Original Message-----