2
votes

I'm rewriting this post to clarify some things and provide a full class definition for the Virtual List I'm having trouble with. The class is defined like so:

from wx import ListCtrl, LC_REPORT, LC_VIRTUAL, LC_HRULES, LC_VRULES, \
  EVT_LIST_COL_CLICK, EVT_LIST_CACHE_HINT, EVT_LIST_COL_RIGHT_CLICK, \
  ImageList, IMAGE_LIST_SMALL, Menu, MenuItem, NewId, ITEM_CHECK, Frame, \
  EVT_MENU

class VirtualList(ListCtrl):
  def __init__(self, parent, datasource = None,
               style = LC_REPORT | LC_VIRTUAL | LC_HRULES | LC_VRULES):
    ListCtrl.__init__(self, parent, style = style)

    self.columns = []
    self.il = ImageList(16, 16)

    self.Bind(EVT_LIST_CACHE_HINT, self.CheckCache)
    self.Bind(EVT_LIST_COL_CLICK, self.OnSort)

    if datasource is not None:
      self.datasource = datasource
      self.Bind(EVT_LIST_COL_RIGHT_CLICK, self.ShowAvailableColumns)

      self.datasource.list = self

      self.Populate()

  def SetDatasource(self, datasource):
    self.datasource = datasource

  def CheckCache(self, event):
    self.datasource.UpdateCache(event.GetCacheFrom(), event.GetCacheTo())

  def OnGetItemText(self, item, col):
    return self.datasource.GetItem(item, self.columns[col])

  def OnGetItemImage(self, item):
    return self.datasource.GetImg(item)

  def OnSort(self, event):
    self.datasource.SortByColumn(self.columns[event.Column])
    self.Refresh()

  def UpdateCount(self):
    self.SetItemCount(self.datasource.GetCount())

  def Populate(self):
    self.UpdateCount()

    self.datasource.MakeImgList(self.il)

    self.SetImageList(self.il, IMAGE_LIST_SMALL)

    self.ShowColumns()

  def ShowColumns(self):
    for col, (text, visible) in enumerate(self.datasource.GetColumnHeaders()):
      if visible:
        self.columns.append(text)
        self.InsertColumn(col, text, width = -2)

  def Filter(self, filter):
    self.datasource.Filter(filter)

    self.UpdateCount()

    self.Refresh()

  def ShowAvailableColumns(self, evt):
    colMenu = Menu()

    self.id2item = {}

    for idx, (text, visible) in enumerate(self.datasource.columns):
      id = NewId()

      self.id2item[id] = (idx, visible, text)

      item = MenuItem(colMenu, id, text, kind = ITEM_CHECK)
      colMenu.AppendItem(item)

      EVT_MENU(colMenu, id, self.ColumnToggle)

      item.Check(visible)

    Frame(self, -1).PopupMenu(colMenu)

    colMenu.Destroy()

  def ColumnToggle(self, evt):
    toggled = self.id2item[evt.GetId()]

    if toggled[1]:
      idx = self.columns.index(toggled[2])

      self.datasource.columns[toggled[0]] = (self.datasource.columns[toggled[0]][0], False)

      self.DeleteColumn(idx)

      self.columns.pop(idx)

    else:
      self.datasource.columns[toggled[0]] = (self.datasource.columns[toggled[0]][0], True)

      idx = self.datasource.GetColumnHeaders().index((toggled[2], True))

      self.columns.insert(idx, toggled[2])

      self.InsertColumn(idx, toggled[2], width = -2)

    self.datasource.SaveColumns()

I've added functions that allow for Column Toggling which facilitate my description of the issue I'm encountering. On the 3rd instance of this class in my application the Column at Index 1 will not display String values. Integer values are displayed properly. If I add print statements to my OnGetItemText method the values show up in my console properly. This behavior is not present in the first two instances of this class, and my class does not contain any type checking code with respect to value display.

It was suggested by someone on the wxPython users' group that I create a standalone sample that demonstrates this issue if I can. I'm working on that, but have not yet had time to create a sample that does not rely on database access. Any suggestions or advice would be most appreciated. I'm tearing my hair out on this one.

2
Are you sure ListCtrl is to blame? Try print item, col, data in OnGetItemText to see when it's calling you and what you're returning.FogleBird
I noticed an interesting pattern in the non-displaying values. In the instance of the list I'm having trouble with the fields that won't display contain string values. The fields that will display contain numeric values. That behavior is not present in the first two instances of my class in my application, and only shows up in the column at index 1 in the third instance. I don't have any type related code in my list class. I'll update this post with a full class definition the next time I'm in front of the code if there aren't other suggestions.g.d.d.c
In case anyone is interested I've generated a standalone sample that demonstrates this behavior available here: groups.google.com/group/wxpython-users/browse_thread/thread/…. Thanks in advance.g.d.d.c
In the interest of due diligence I installed the sample application above on two more machines. One is Windows 7 running the 64-bit versions of wx/Python, and the other is Windows 7 running the 32-bit versions. My machine is XP 64-bit and has the 32-bit binaries installed. All three machines exhibit the same behavior using the stand alone sample.g.d.d.c

2 Answers

0
votes

Are you building on the wxPython demo code for virtual list controls? There are a couple of bookkeeping things you need to do, like set the ItemCount property.

One comment about your OnGetItemText method: Since there's no other return statement, it will return None if data is None, so your test has no effect.

How about return data or "" instead?

0
votes

There's a problem with the native object in Windows. If GetImg returns None instead of -1 the list has a problem with column 1 for some reason. That from Robin over on the Google Group post for this issue.