[wxPython] Q: Elegant coding for collections of similar controls

What are the nicest ways of creating a collection of controls?

This is one way that I have used, to indicate what I mean. In this
case ButtonColumn is a container for a collection of objects
subclassed from wxButton. The definition of these objects is
nested within the container simply to hide them.

This construction makes it possible to create a list of object
references with distinct event sinks.

class ButtonColumn ( wxPanel ):
    class aButton ( wxButton ):
        def __init__ ( self, parent, label, whotocall ):
            id = wxNewId ( )
            wxButton.__init__ ( self, parent, id, label, ...)
            self.whotocall = whotocall
            EVT_BUTTON ( self, id, self.OnClick )

        def OnClick ( self, event ):
            if self.whotocall: self.whotocall ( )

    def __init__ ( self, parent, width, buttons, Bottom = 0 ):
        wxPanel.__init__ ( self, parent, -1, ... )
        self.parent = parent
        previous = None
        for button in buttons [ 0 : -Bottom ]:
            oneButton = self.aButton ( self, button [ 1 ], button [ 2 ] )
            lc = wxLayoutConstraints ( )
            lc.left.SameAs ( self, wxLeft, 5 )
            lc.right.SameAs ( self, wxRight, 5 )
            lc.height.AsIs ( )
            if previous: lc.top.SameAs ( previous, wxBottom, 5 )
            else: lc.top.SameAs ( self, wxTop, 5 )
            oneButton.SetConstraints ( lc )
            previous = oneButton

This object is initialised in the following way.

            buttons = [ ( 'ok', 'OK', self.OKClicked, ),
                        ( 'cancel2', 'Cancel', self.CancelClicked, ),
                        ( 'exit2', 'Exit', self.ExitClicked, ),
                        ( 'reinvert2', 'Re-invert', self.ReinvertClicked, ),
                        ]

            self.tp = ButtonColumn ( self, 45, buttons, Bottom = 2 )

I would like to know what others do in this circumstance. What do
you do?

Thanks in advance.

Bill

What are the nicest ways of creating a collection of controls?

This is one way that I have used, to indicate what I mean. In this
case ButtonColumn is a container for a collection of objects
subclassed from wxButton. The definition of these objects is
nested within the container simply to hide them.

Feel free to add things like this to the Cookbook at
    http://wxpython.org/cgi-bin/wiki/wxPython_20Cookbook

···

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

Bill,

Have you looked at the PythonCard project? They are storing all widget
information in a resource file (basically a python dictionary at this
point). You might find some ideas there, and you may even want to get
involved in the development.

PythonCard is at http://sourceforge.net/projects/pythoncard/

···

---
Patrick K. O'Brien
Orbtech
"I am, therefore I think."

-----Original Message-----
From: wxpython-users-admin@lists.wxwindows.org
[mailto:wxpython-users-admin@lists.wxwindows.org]On Behalf Of Bill Bell
Sent: Tuesday, August 21, 2001 2:48 PM
To: wxpython-users@lists.wxwindows.org
Subject: [wxPython] Q: Elegant coding for collections of similar controls

What are the nicest ways of creating a collection of controls?

This is one way that I have used, to indicate what I mean. In this
case ButtonColumn is a container for a collection of objects
subclassed from wxButton. The definition of these objects is
nested within the container simply to hide them.

This construction makes it possible to create a list of object
references with distinct event sinks.

class ButtonColumn ( wxPanel ):
    class aButton ( wxButton ):
        def __init__ ( self, parent, label, whotocall ):
            id = wxNewId ( )
            wxButton.__init__ ( self, parent, id, label, ...)
            self.whotocall = whotocall
            EVT_BUTTON ( self, id, self.OnClick )

        def OnClick ( self, event ):
            if self.whotocall: self.whotocall ( )

    def __init__ ( self, parent, width, buttons, Bottom = 0 ):
        wxPanel.__init__ ( self, parent, -1, ... )
        self.parent = parent
        previous = None
        for button in buttons [ 0 : -Bottom ]:
            oneButton = self.aButton ( self, button [ 1 ], button [ 2 ] )
            lc = wxLayoutConstraints ( )
            lc.left.SameAs ( self, wxLeft, 5 )
            lc.right.SameAs ( self, wxRight, 5 )
            lc.height.AsIs ( )
            if previous: lc.top.SameAs ( previous, wxBottom, 5 )
            else: lc.top.SameAs ( self, wxTop, 5 )
            oneButton.SetConstraints ( lc )
            previous = oneButton

This object is initialised in the following way.

            buttons = [ ( 'ok', 'OK', self.OKClicked, ),
                        ( 'cancel2', 'Cancel', self.CancelClicked, ),
                        ( 'exit2', 'Exit', self.ExitClicked, ),
                        ( 'reinvert2', 'Re-invert', self.ReinvertClicked, ),
                        ]

            self.tp = ButtonColumn ( self, 45, buttons, Bottom = 2 )

I would like to know what others do in this circumstance. What do
you do?

Thanks in advance.

Bill

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