wxPython and wxFlexGridSizer problem

Cliff Wells logiplexsoftware at earthlink.net
Tue Mar 26 17:56:59 EST 2002


On 26 Mar 2002 21:40:17 GMT
Uwe Schmitt wrote:

> Cliff Wells <logiplexsoftware at earthlink.net> wrote:
> 
> | That may be the case, but Jeff brought up some good points.  It looks
odd
> | to create a window in this fashion (it looks procedural rather than
OO).  A
> | more typical approach would be something like:
> 
> I know what you mean, but I'd like to write a general procedure
> which generates an input/output mask based on a relational database
> describption. And this mask may appear in a wxFrame *or* a wxDialog.
> So I use the "parent" of the mask as a parameter of my function...
 
So why not derive from wxPanel rather than wxFrame or wxDialog and then
place the panel in a frame or dialog depending upon your needs?  You could
pass a list of the fields you want as an argument to __init__.  For
example:

class InputPanel(wxPanel):
    def __init__(self, parent, id, fields):
        wxPanel.__init__(self, parent, id)
        gridSizer= wxFlexGridSizer(cols=2)

        for label, size in fields:
            t = wxStaticText(self, -1, label)
            tt = wxTextCtrl(self, -1, "", size=wxSize(size,-1))
            gridSizer.AddMany([
                (t, 1, wxALL | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL, 3),
                (tt, 1, wxALL |wxALIGN_LEFT, 3)
                ])

        gridSizer.SetSizeHints(self)
        gridSizer.Fit(self)
        self.SetSizer(gridSizer)
        self.SetAutoLayout(true)
        self.Layout()

class InputDialog(wxDialog):
    def __init__(self, parent, id, fields):
        wxDialog.__init__(self, parent, id, "")
        sizer = wxBoxSizer(wxVERTICAL)
        self.SetSizer(sizer)
        self.SetAutoLayout(true)
        panel = InputPanel(self, -1, fields)
        sizer.Add(panel, 1, wxEXPAND)
        sizer.Fit(self)
        
app = wxPySimpleApp()


fields = [("Name", 100),
          ("Address", 200),
          ("Phone", 20)]

window = InputDialog(None, -1, fields)
window.ShowModal()

To have it in a frame, simply create another class similar to InputDialog
that uses a wxFrame instead.  With this, you can also embed the panel in
something else, for instance a wxNotebook.

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308




More information about the Python-list mailing list