Hello. I hope this is useful to someone. I've found what (I feel) is a limitation with
the column-clicking/sorting feature(s) of wxListCtrl. What it boils down to is this:
You have a tuple, list, or dictionary, and you want to add values from given indices
to your wxListCtrl. Furthermore, you want to be able to sort the different columns with
OnColClick.
Here's the problem: suppose your list contains 20 items, but you only want
to add 4 of them to the ListCtrl. On top of that, you may want to a) add items beyond
the 4th index - i.e., List[3] - (remember, you only have 4 columns), or b) you want to
add them out of order - i.e., List[2], List[0], List[3]... In each of these two cases, you're
asking for weird behavior to result from column clicking/sorting.
Solution: make sure that all of the data you want to put into the ListCtrl is a) at or below the
List's index number which corresponds to the number of columns in your ListCtrl minus 1.
The code below uses a dictionary of key/value pairs- in this case we're talking
about the index of the item in the VALUES list. Key doesn't seem to matter. And lest I forget,
b) make sure you add the items to the ListCtrl in the order they are found in your original data
structure.
hope this helps, if not let me know so we can pinpoint this problem. I really like ListCtrl and would
hate to give it up.
CODE:
#!/usr/bin/env python
### this is just a test script showing a limitation in the
### column clicking feature of wxListCtrl for wxPython
from wxPython.wx import *
musicdata = {
12 : ["George", "Praying For Time", "Silly","aaaaa", "ksadjflkjasdf"],
3 : ("Bad", "The Price Of Love", "SpeakEnglishOrDie", "bbbbb","kfdkjdkjdwwwww"),
1 : ("DNA", "Tom's Diner", "Rock", "ccccc", "mmmmm"),
4 : ("Gloria", "Here We Are", "Rock", "gggggg", "wwqfkdsjf"),
}
class ListCtrlTest(wxFrame):
def __init__(self, parent, ID, title, position, size):
wxFrame.__init__(self, parent, ID, title,
wxDefaultPosition, wxSize(429, 333))
tID = wxNewId()
self.list = wxListCtrl(self, tID,
style=wxLC_REPORT|wxSUNKEN_BORDER)
self.list.InsertColumn(0, "Artist")
self.list.InsertColumn(1, "Title", wxLIST_FORMAT_RIGHT)
self.list.InsertColumn(2, "Genre")
self.list.InsertColumn(3, "Garbage")
items = musicdata.items()
for x in range(len(items)):
key, data = items[x]
self.list.InsertStringItem(x, data[0])
self.list.SetStringItem(x, 1, data[1]) ## 0<1<2<3 works
self.list.SetStringItem(x, 2, data[2])
self.list.SetStringItem(x, 3, data[3])
self.list.SetItemData(x, key)
# self.list.InsertStringItem(x, data[0])
# self.list.SetStringItem(x, 1, data[3]) ## 0<3<1<2 doesn't work
# self.list.SetStringItem(x, 2, data[1]) ## items out of order
# self.list.SetStringItem(x, 3, data[2])
# self.list.SetItemData(x, key)
# self.list.InsertStringItem(x, data[0])
# self.list.SetStringItem(x, 1, data[1]) ## 0<1<2<4 doesn't work.
# self.list.SetStringItem(x, 2, data[2]) ## items out of range
# self.list.SetStringItem(x, 3, data[4])
# self.list.SetItemData(x, key)
# self.list.InsertStringItem(x, data[0])
# self.list.SetStringItem(x, 1, data[1])
# self.list.SetStringItem(x, 2, data[4]) ## 0<1<4<2 doesn't work.
# self.list.SetStringItem(x, 3, data[2]) ## items out of order and out of range
# self.list.SetItemData(x, key)
self.list.SetColumnWidth(0, wxLIST_AUTOSIZE)
self.list.SetColumnWidth(1, wxLIST_AUTOSIZE)
self.currentItem = 0
EVT_LIST_ITEM_SELECTED(self, tID, self.OnItemSelected)
EVT_LIST_ITEM_ACTIVATED(self, tID, self.OnItemActivated)
EVT_LIST_COL_CLICK(self, tID, self.OnColClick)
def OnItemSelected(self, event):
self.currentItem = event.m_itemIndex
def OnItemActivated(self, event):
self.currentItem = event.m_itemIndex
def OnColClick(self, event):
self.col = event.m_col
self.list.SortItems(self.ColumnSorter)
def ColumnSorter(self, key1, key2):
item1 = musicdata[key1][self.col]
item2 = musicdata[key2][self.col]
if item1 == item2: return 0
elif item1 < item2: return -1
else: return 1
class MyApp(wxApp):
def OnInit(self):
frame = ListCtrlTest(NULL, -1, "ListCtrl Test", wxPoint(200,200), wxSize(429,384))
frame.Show(true)
self.SetTopWindow(frame)
return true
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
···
_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users