Question

I am trying to sort items in a 3 column list but it's not quite working. I have it set up to populate a "table". When I first run the program, it sorts the columns no problem. However, I have an option to select another data source and the same table is populated with new data (mostly the 3rd column changes). However, now when I sort the items, it sorts based on the original data and not the newly shown data. Any help?

My code (some parts removed as not necessary):

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

Again, the 3-column "table" updates the data appropriately so no problem there. The only issue is when I try to sort the columns, it always sorts it based on the original "adpdata" that was used to fill in the table.

Any ideas?

EDIT:

Here's two example dicts:

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 is my default dictionary that I use. When I build the lists, I can easily sort correctly the three columns (say column 3 would show in order 1,2,3,4,5. However, when I switch to Selection_B, it still sorts based on Selection_A values (but the lists shows Selection_B values) -- for example it shows 3,5,1,2,4 for column 3 and A,B,C,D,E for column 1 sorted (should be 1,2,3,4,5 and C,D,A,E,B). I don't know if this helps? I can't show my original dicts because there is so much data.

ALSO, I did check -- my self.itemDataMap does update appropriately (I printed the results) -- the sorting just doesn't take place. I guess I'm not completely sure where the "sort" values are "stored/updated"?

Was it helpful?

Solution

I'm not entirely sure why that doesn't work, but I think it might have something to do with your resetting the self.itemDataMap variable. I wrote the following quick and dirty script and it works:

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()

Try commenting out the self.itemDataMap at the end of OnselectADP. If that doesn't work, then the next thing I would try is removing the global stuff.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top