MVC programming with python (newbie) - please help

Frank Niessink frank at
Fri Jan 6 17:36:04 EST 2006

bwaha wrote:
> At some level this seems to me like the class ListDataModel above. I just
> need to make a MyTreeControl class. However here GS(et) routines are
> implemented in the ProjectFileDecoder class (data model?)  whereas in the
> earlier advice they are in class MyCoolListControl. So I'm not understanding
> how data gets from the DataModel class to the ListControl class. After all
> the words I've said,  I think that's the core misunderstanding I have.
> Hope someone can help clear that up for me.

You may want to look at the wxPython "virtual" ListCtrl (see the 
wxPython demo). Whenever a virtual ListCtrl is refreshed it will call 
specific methods on itself to get the appropriate content, e.g. 
OnGetItemText(self, rowIndex, columnIndex). You can do whatever you want 
in that method as long as you return the text that has to be displayed 
in the row with index rowIndex and the column with index columnIndex. 
One option would be to subclass wx.ListCtrl and provide something like 
your ListDataModel, like this (tested):

import wx

class MyCoolListControl(wx.ListCtrl):
     def __init__(self, *args, **kwargs):
         # don't pass the model argument to wx.ListCtrl:
         self._model = kwargs.pop('model')
         # force virtual mode:
         kwargs['style'] = wx.LC_VIRTUAL|kwargs['style']
         super(MyCoolListControl, self).__init__(*args, **kwargs)

     def OnGetItemText(self, rowIndex, columnIndex=0):
         return self._model.getItemText(rowIndex, columnIndex)

     # and similarly for OnGetItemImage and OnGetItemAttr

class ListDataModel(object):
     def getItemText(self, rowIndex, columnIndex):
         return 'Text in row %d, column %d'%(rowIndex, columnIndex)

app = wx.App(0)
model = ListDataModel()
window = wx.Frame(None)
cool = MyCoolListControl(window, model=model, style=wx.LC_LIST)
cool.SetItemCount(1000000) # This triggers a refresh of the list ctrl

In a similar vein you could use this scheme for wx.TreeCtrl, though that 
requires (much) more work because the TreeCtrl has no build-in virtual 
mode. That's what I did for the TreeListCtrl in Task Coach 
(, see

HTH, Frank

More information about the Python-list mailing list