custom control design question

Hi everyone,

I am making a custom CheckListBox control, which allows you uncheck/check all of the items with two HyperLinkCtrls. To do this I subclassed from a panel and added my elements. However, to the user I would like this to appear as a CheckListBox basically, so that calls to its (and ListBox's) methods can be done on the instance of the CustomCheckListBox. Is there a nice pythonic way to do this? Does this come up ever? I guess what I see most controls like this doing in wxPython is exposing their inner control, so in this case users would do CustomCheckListBox.ListBox, but I was wondering if there might be a different solution. I thought of overriding __getattr__ to return the __getattr__ of the listbox, but I am not sure how I feel about that. Here is my control to get perhaps a clearer idea:

class CustomCheckListBox(wx.Panel):
    def __init__(self, parent, *args, **kwargs):
        wx.Panel.__init__(self, parent)
        self.numItems = 0
               self.listbox = wx.CheckListBox(self, *args, **kwargs)
        selectAll = wx.HyperlinkCtrl(self, -1, "All", "All")
        selectNone = wx.HyperlinkCtrl(self, -1, "None", "None")
        #don't make them change colors after you click them
        for hyperLink in [selectAll, selectNone]:
            hyperLink.VisitedColour = hyperLink.NormalColour
               selectSizer = wx.BoxSizer()
        selectSizer.Add(wx.StaticText(self, label="Select: "))
        selectSizer.Add(selectAll)
        selectSizer.Add(wx.StaticText(self, label=" | "))
        selectSizer.Add(selectNone)
               mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(selectSizer, 0, wx.ALIGN_CENTER_HORIZONTAL)
        mainSizer.Add(self.listbox, 1, wx.EXPAND|wx.TOP, 1)
               self.Sizer = mainSizer
        mainSizer.Layout()
               self.Bind(wx.EVT_HYPERLINK, self.onSelect)
           def onSelect(self, event):
        check = event.URL == "All"
        for i in range(self.numItems):
            self.listbox.Check(i, check)
           def Set(self, lst):
        self.numItems = len(lst)
        return self.listbox.Set(lst)

Thanks for your time,
Mike

Mike Rooney wrote:

Hi everyone,

I am making a custom CheckListBox control, which allows you uncheck/check all of the items with two HyperLinkCtrls. To do this I subclassed from a panel and added my elements. However, to the user I would like this to appear as a CheckListBox basically, so that calls to its (and ListBox's) methods can be done on the instance of the CustomCheckListBox. Is there a nice pythonic way to do this? Does this come up ever? I guess what I see most controls like this doing in wxPython is exposing their inner control, so in this case users would do CustomCheckListBox.ListBox, but I was wondering if there might be a different solution. I thought of overriding __getattr__ to return the __getattr__ of the listbox, but I am not sure how I feel about that.

That *is* a pythonic way to do delegation. Well almost. I'd do it like this:

  def __getattr__(self, name):
    return getattr(self.listbox, name)

The only problem with that is that you may end up delegating some methods that you don't want to. Another way to do delegation of course is to just explicitly expose just those methods you want, and in each of them call the listbox method:

  def IsSelected(self, *a, **kw):
    return self.listbox.IsSelected(*a, **kw)

···

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