I modified Mike’s example for column sorting, and turned it into this… it works for the small dataset, but when you increase the number of rows it starts showing blanks:
import wx
import wx.lib.mixins.listctrl as listmix
musicdata = [
{‘Artist’:“Bad English”, ‘Title’:“The Price Of Love”, ‘Genre’:“Rock”},
{‘Artist’:“DNA featuring Suzanne Vega”, ‘Title’:“Tom’s Diner”, ‘Genre’:“Rock”},
{‘Artist’:“George Michael”, ‘Title’:“Praying For Time”, ‘Genre’:“Rock”},
{‘Artist’:“Gloria Estefan”, ‘Title’:“Here We Are”, ‘Genre’:“Rock”},
{‘Artist’:“Linda Ronstadt”, ‘Title’:“Don’t Know Much”, ‘Genre’:“Rock”},
{‘Artist’:“Michael Bolton”, ‘Title’:“How Am I Supposed To Live Without You”, ‘Genre’:“Blues”},
{‘Artist’:“Paul Young”, ‘Title’:“Oh Girl”, ‘Genre’:“Rock”},
]
···
########################################################################
class SortableColumnListCtrl(wx.ListCtrl, listmix.ColumnSorterMixin):
#----------------------------------------------------------------------
def init(self, parent, headers, ID=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0):
wx.ListCtrl.init(self, parent, ID, pos, size, style)
Now that the list exists we can init the other base class,
see wx/lib/mixins/listctrl.py
listmix.ColumnSorterMixin.init(self, 3)
self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self)
self.itemDataMap = None
self.headers = sorted(headers)
self._generate_headers()
def _generate_headers(self):
col_num = 0
for header in self.headers:
self.InsertColumn(col_num, header)
col_num += 1
#----------------------------------------------------------------------
Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
def GetListCtrl(self):
return self
#----------------------------------------------------------------------
def OnColClick(self, event):
print “column clicked”
event.Skip()
def set_data(self, data):
if self.ItemCount:
self.ClearAll()
self._generate_headers()
#self.itemDataMap = data
self.itemDataMap = {}
items = data.items()
index = 0
for data_dict in data:
assert len(data_dict) == self.ColumnCount
sorted_row = sorted(data_dict.items(), key=lambda t: t[0])
self.itemDataMap[index] = [str(tup[1]) for tup in sorted_row]
i = self.Append(self.itemDataMap[index])
print self.itemDataMap[index]
self.InsertStringItem(index, self.itemDataMap[index][0])
print self.itemDataMap[index][0],
for x in range(1, len(data_dict)):
self.SetStringItem(index, x, str(self.itemDataMap[index]))
print str(self.itemDataMap[index]),
print ‘’
self.SetItemData(i, index)
index += 1
print index
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def init(self):
wx.Frame.init(self, None, wx.ID_ANY, “List Control Tutorial”)
Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, -1, style=wx.WANTS_CHARS)
self.list_ctrl = SortableColumnListCtrl(panel, size=(-1,100),
style=wx.LC_REPORT
wx.BORDER_SUNKEN
wx.LC_SORT_ASCENDING,
headers=musicdata[0].keys()
)
self.list_ctrl.set_data(musicdata*20)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
panel.SetSizer(sizer)
#----------------------------------------------------------------------
Run the program
if name == “main”:
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
``
Any ideas on why these non-zero columns on all but the last row aren’t showing?