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