对UltimateList中的项目排序Ctrl

我想创建一个最终的基于列表ctrl的表,用户可以通过单击列标题对列进行排序。下面是我尝试的代码:

选择 | 换行 | 行号
  1. import wx
  2. import wx.lib.mixins.listctrl as listmix
  3. from wx.lib.agw import ultimatelistctrl as ULC
  4.  
  5. APPNAME='Sortable Ultimate List Ctrl'
  6. APPVERSION='1.0'
  7. MAIN_WIDTH=300
  8. MAIN_HEIGHT=300
  9.  
  10. musicdata = {
  11. 0 : ("Bad English", "The Price Of Love"),
  12. 1 : ("DNA featuring Suzanne Vega", "Tom's Diner"),
  13. 2 : ("George Michael", "Praying For Time"),
  14. 3 : ("Gloria Estefan", "Here We Are"),
  15. 4 : ("Linda Ronstadt", "Don't Know Much"),
  16. 5 : ("Michael Bolton", "How Am I Supposed To Live Without You"),
  17. 6 : ("Paul Young", "Oh Girl"),
  18. }
  19.  
  20. ########################################################################
  21. class TestUltimateListCtrlPanel(wx.Panel, listmix.ColumnSorterMixin):
  22.  
  23.     #----------------------------------------------------------------------
  24.     def __init__(self, parent):
  25.         wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS, size=(MAIN_WIDTH,MAIN_HEIGHT))
  26.  
  27.         self.index = 0
  28.  
  29.         self.list_ctrl = ULC.UltimateListCtrl(self, -1, agwStyle=ULC.ULC_REPORT|ULC.ULC_HAS_VARIABLE_ROW_HEIGHT)
  30.         self.list_ctrl.InsertColumn(0, "Artist")
  31.         self.list_ctrl.InsertColumn(1, "Title", wx.LIST_FORMAT_RIGHT)
  32.         self.list_ctrl.InsertColumn(2, "Download")
  33.  
  34.         items = musicdata.items()
  35.         index = 0
  36.         for key, data in items:
  37.             pos=self.list_ctrl.InsertStringItem(index, data[0])
  38.             self.list_ctrl.SetStringItem(index, 1, data[1])
  39.             #self.list_ctrl.SetStringItem(index, 2, data[2])
  40.             button = wx.Button(self.list_ctrl, id=wx.ID_ANY, label="Download")
  41.         self.list_ctrl.SetItemWindow(pos, col=2, wnd=button, expand=True)
  42.             self.list_ctrl.SetItemData(index, key)
  43.             index += 1
  44.  
  45.         # Now that the list exists we can init the other base class,
  46.         # see wx/lib/mixins/listctrl.py
  47.         self.itemDataMap = musicdata
  48.         listmix.ColumnSorterMixin.__init__(self, 3)
  49.         self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list_ctrl)
  50.  
  51.         sizer = wx.BoxSizer(wx.VERTICAL)
  52.         sizer.Add(self.list_ctrl, 1, wx.ALL|wx.EXPAND, 5)
  53.         self.SetSizer(sizer)
  54.  
  55.  
  56.     def GetListCtrl(self):
  57.         return self.list_ctrl
  58.  
  59.     #----------------------------------------------------------------------
  60.     def OnColClick(self, event):
  61.         pass
  62.  
  63. ########################################################################
  64. class MyForm(wx.Frame):
  65.  
  66.     #----------------------------------------------------------------------
  67.     def __init__(self):
  68.         wx.Frame.__init__(self,None,wx.ID_ANY,'%s v%s' % (APPNAME,APPVERSION),size=(MAIN_WIDTH,MAIN_HEIGHT),style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN)
  69.  
  70.         # Add a panel so it looks the correct on all platforms
  71.         panel = TestUltimateListCtrlPanel(self)
  72.  
  73. #----------------------------------------------------------------------
  74. # Run the program
  75. if __name__ == "__main__":
  76.     app = wx.App(False)
  77.     frame = MyForm()
  78.     frame.Show()
  79.     app.MainLoop()

单击该列不会对"艺术家"或"标题"字段进行排序。我希望能够在用户单击列标题时对它们进行排序。请帮忙。这就是它的样子

标签: python

添加新评论