使用Python中的listctrl和wxpython对项目进行排序

时间:2013-08-05 19:07:40

标签: python wxpython listctrl columnsorting

我正在尝试对3列列表中的项目进行排序,但它并不是很有用。我把它设置为填充“表”。当我第一次运行程序时,它对列进行排序没有问题。但是,我可以选择另一个数据源,同一个表填充新数据(主要是第3列更改)。但是,现在当我对项目进行排序时,它会根据原始数据而不是新显示的数据进行排序。有什么帮助吗?

我的代码(某些部分已删除,不需要):

class EditADP(wx.Dialog):

    def __init__(self, parent, title):
        super(EditADP, self).__init__(parent=parent, 
            title=title, size=(450, 700))

        #Sizer Info Here

        # This is where user selects new data source

        try:
            self.selectADP = wx.ComboBox(self,-1,tmp_default_ADP,choices=ADP_Options,style=wx.CB_READONLY)
        except:
            self.selectADP = wx.ComboBox(self,-1,default_ADP,choices=ADP_Options,style=wx.CB_READONLY)

        #More Sizers

        self.Bind(wx.EVT_COMBOBOX, self.OnselectADP, self.selectADP)

        tmp_ADPselection = default_ADP

        global adpdata

        adpdata = {}
        for i in Master_PlayerID:
            adpdata[i] = (Master_1[i],Master_2[i],Master_3[tmp_ADPselection][i],i)

        self.ADPlist = EditableListCtrl_editADP(self,-1,style=wx.LC_REPORT)
        self.ADPlist.InsertColumn(1,'Col1',format=wx.LIST_FORMAT_CENTRE)
        self.ADPlist.InsertColumn(2,'Col2',format=wx.LIST_FORMAT_CENTRE)
        self.ADPlist.InsertColumn(3,'Col3',format=wx.LIST_FORMAT_CENTRE)

        items = adpdata.items()
        index = 0

        for key, data in items:
            self.ADPlist.InsertStringItem(index, Master_List[data[3]])
            self.ADPlist.SetStringItem(index, 1, data[1])
            self.ADPlist.SetStringItem(index, 2, str(data[2]))
            self.ADPlist.SetItemData(index, key)
            index += 1

        self.itemDataMap = adpdata

        # More sizer and button stuff here

    def OnselectADP(self, event):
        tmp_ADPselection = ADP_Options[event.GetSelection()]

        self.ADPlist.DeleteAllItems()        

        global adpdata

        adpdata = {}
        for i in Master_PlayerID:
            adpdata[i] = (Master_1[i],Master_2[i],Master_3[tmp_ADPselection][i],i)

        items = adpdata.items()
        index = 0

        for key, data in items:
            self.ADPlist.InsertStringItem(index, Master_List[data[3]])
            self.ADPlist.SetStringItem(index, 1, data[1])
            self.ADPlist.SetStringItem(index, 2, str(data[2]))
            self.ADPlist.SetItemData(index, key)
            index += 1        

        self.itemDataMap = adpdata

class EditableListCtrl_editADP(wx.ListCtrl, listmix.TextEditMixin, listmix.ColumnSorterMixin):

    def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=0):
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
        listmix.TextEditMixin.__init__(self)
        listmix.ColumnSorterMixin.__init__(self, len(adpdata))
        self.itemDataMap = adpdata

    def GetListCtrl(self):
        return self

同样,3列“表”适当地更新数据,因此没有问题。唯一的问题是当我尝试对列进行排序时,它总是根据用于填充表格的原始“adpdata”对其进行排序。

有什么想法吗?

编辑:

以下是两个例子:

Selection_A = {1:[A, no, 1],2:[B,no,2],3:[C,yes,3],4:[D,yes,4],5:[E,no,5]}
Selection_B = {1:[A, no, 3],2:[B,no,5],3:[C,yes,1],4:[D,yes,2],5:[E,no,4]}

Selection_A是我使用的默认字典。当我构建列表时,我可以很容易地正确排序三列(比如第3列将按顺序显示1,2,3,4,5。但是,当我切换到Selection_B时,它仍然根据Selection_A值进行排序(但是列表显示Selection_B值) - 例如,它显示列3的3,5,1,2,4和列1的A,B,C,D,E排序(应为1,2,3,4,5和C,D,A,E,B)。我不知道这是否有帮助?我无法显示原始的词汇,因为数据太多了。

另外,我确实检查过 - 我的self.itemDataMap确实更新了(我打印了结果) - 排序不会发生。我想我不完全确定“存储/更新”“排序”值的位置?

1 个答案:

答案 0 :(得分:1)

我不完全确定为什么这不起作用,但我认为它可能与重置self.itemDataMap变量有关。我编写了以下快速而又脏的脚本,它可以工作:

import wx
import wx.lib.mixins.listctrl as listmix

########################################################################
class SortableListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.ListCtrl.__init__(self, parent, size=(-1,100),
                             style=wx.LC_REPORT|wx.BORDER_SUNKEN)
        listmix.ListCtrlAutoWidthMixin.__init__(self)


########################################################################
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, wx.ID_ANY)
        self.index = 0

        data = {1: ["Dr appointment", "05/06/2014", ""],
                2: ["Meeting", "08/22/2013", "Board Room"]
                }

        self.list_ctrl = SortableListCtrl(panel)
        self.list_ctrl.InsertColumn(0, 'Subject')
        self.list_ctrl.InsertColumn(1, 'Due')
        self.list_ctrl.InsertColumn(2, 'Location', width=125)

        index = 0
        for d in data:
            print data[d][0]
            print data[d][1]
            self.list_ctrl.InsertStringItem(index, data[d][0]),
            self.list_ctrl.SetStringItem(index, 1, data[d][1])
            self.list_ctrl.SetStringItem(index, 2, data[d][2])
            index += 1

        btn = wx.Button(panel, label="Update")
        btn.Bind(wx.EVT_BUTTON, self.updateListCtrl)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 1, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.CENTER|wx.ALL, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def updateListCtrl(self, event):
        """
        """
        self.list_ctrl.DeleteAllItems()
        data = {1: ["Tennis", "09/06/2013", ""],
                2: ["Lunch", "08/15/2013", "Chick-fil-a"]
                }

        index = 0
        for d in data:
            print data[d][0]
            print data[d][1]
            self.list_ctrl.InsertStringItem(index, data[d][0]),
            self.list_ctrl.SetStringItem(index, 1, data[d][1])
            self.list_ctrl.SetStringItem(index, 2, data[d][2])
            index += 1

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

尝试在OnselectADP末尾注释掉self.itemDataMap。如果这不起作用,那么接下来我会尝试删除全局内容。