我在字典中有一个数据将转到 ListCtrl。我有复选框 listmix.CheckListCtrlMixin 用于选择数据和 listmix.ColumnSorterMixin 用于排序。
问题是,在我按任何列对数据进行排序后,数据的原始顺序发生了变化,并且在检查了项目后,我找不到数据以进行进一步的操作。
是否有可能获得新的“排序后”字典或类似的东西?
这是演示示例。
import sys
import wx
import wx.lib.mixins.listctrl as listmix
musicdata = {
1 : ( 'Bad English', 'The Price Of Love', 'Rock' ),
2 : ( 'Michael Bolton', 'How Am I Supposed To Live Without You', 'Blues' ),
3 : ( 'Paul Young', 'Oh Girl', 'Rock' ),
4 : ( 'Paula Abdul', 'Opposites Attract', 'Rock' ),
5 : ( 'Richard Marx', 'Should\'ve Known Better', 'Rock' ),
6 : ( 'Rod Stewart', 'Forever Young', 'Rock' ),
7 : ( 'Roxette', 'Dangerous', 'Rock' ),
}
class TestListCtrl( wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.CheckListCtrlMixin ) :
def __init__( self, parent, ID, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0 ) :
wx.ListCtrl.__init__( self, parent, ID, pos, size, style )
listmix.ListCtrlAutoWidthMixin.__init__( self )
listmix.CheckListCtrlMixin.__init__(self)
self.Bind(wx.EVT_LIST_COL_CLICK, self.OnCheckItem)
def OnCheckItem(self, data, flag):
print(data, flag)
class TabPanel( wx.Panel, listmix.ColumnSorterMixin ) :
def __init__( self, parent ) :
wx.Panel.__init__( self, parent=parent, id=wx.ID_ANY )
self.createAndLayout()
def createAndLayout( self ) :
sizer = wx.BoxSizer( wx.VERTICAL )
self.list = TestListCtrl( self, wx.ID_ANY, style=wx.LC_REPORT
| wx.BORDER_NONE
| wx.LC_EDIT_LABELS
| wx.LC_SORT_ASCENDING )
sizer.Add( self.list, 1, wx.EXPAND )
self.populateList()
self.itemDataMap = musicdata
listmix.ColumnSorterMixin.__init__( self, 3 )
self.SetSizer( sizer )
self.SetAutoLayout( True )
def populateList( self ) :
self.list.InsertColumn( 0, 'Artist' )
self.list.InsertColumn( 1, 'Title', wx.LIST_FORMAT_RIGHT )
self.list.InsertColumn( 2, 'Genre' )
items = musicdata.items()
for key, data in items :
index = self.list.InsertStringItem( sys.maxint, data[ 0 ] )
self.list.SetStringItem( index, 1, data[ 1 ] )
self.list.SetStringItem( index, 2, data[ 2 ] )
self.list.SetItemData( index, key )
self.list.SetColumnWidth( 0, wx.LIST_AUTOSIZE )
self.list.SetColumnWidth( 1, wx.LIST_AUTOSIZE )
self.list.SetColumnWidth( 2, 100 )
self.list.SetItemState( 5, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED )
self.currentItem = 0
def GetListCtrl( self ) :
return self.list
class DemoFrame( wx.Frame ) :
def __init__( self ) :
wx.Frame.__init__( self, None, wx.ID_ANY, title="Panel Tutorial",
size=(600, 300) )
panel = TabPanel( self )
self.Show()
app = wx.App()
frame = DemoFrame()
app.MainLoop()