Hi-
I'm using wxPython 2.4.0.1 on Windows 2000.
I've included a small example which includes
a wxListCtrl, and shows how to control
the sorting of columns.
To get it to work I had to make the following
call:
self.list.SetItemData(0, i)
My questions are:
a) Why is this call necessary?
b) What exactly does it do?
c) Is this the "correct" way to handle sorting?
Many thanks in advance,
Dave
···
**************************************************************
from wxPython.wx import *
import whrandom
def getRandomString():
s = ""
for i in range(0,5):
s = s + chr(whrandom.randint(97,122)) #range 97 to 122 corresponds to
ASCII 'a' to 'z'
return s
class SortExample(wxApp):
def OnInit(self):
frame = wxFrame(NULL, -1, "Sort Example")
self.list=wxListCtrl(frame, wxNewId(),
style=wxLC_REPORT|wxSUNKEN_BORDER)
self.list.Show(true)
self.list.InsertColumn(0, "Integer")
self.list.InsertColumn(1, "Positive Float")
self.list.InsertColumn(2, "Negative Float")
self.list.InsertColumn(3, "String")
self.data = []
for i in range(0,25):
tempTuple = ( whrandom.randint(0,100), whrandom.random(),
-whrandom.random(), getRandomString() )
self.data.append(tempTuple)
self.list.InsertStringItem(0, str(tempTuple[0]))
self.list.SetStringItem(0, 1, str(tempTuple[1]))
self.list.SetStringItem(0, 2, str(tempTuple[2]))
self.list.SetStringItem(0, 3, str(tempTuple[3]))
self.list.SetItemData(0, i)
self.list.SetColumnWidth(0, wxLIST_AUTOSIZE_USEHEADER)
self.list.SetColumnWidth(1, wxLIST_AUTOSIZE)
self.list.SetColumnWidth(2, wxLIST_AUTOSIZE)
self.list.SetColumnWidth(3, wxLIST_AUTOSIZE)
#set to 0,1,2 or 3 to sort by the corresponding column
self.sortColumn = 3
self.list.SortItems(self.compareData)
frame.Show(true)
self.SetTopWindow(frame)
return true
def compareData(self, rowID1, rowID2):
print rowID1, rowID2
item1 = self.data[rowID1][self.sortColumn]
item2 = self.data[rowID2][self.sortColumn]
return cmp(item1, item2)
se = SortExample(0)
se.MainLoop()