ListBox, LB_SORT, GetSelections()

Hi,

If you set the style to "LB_SORT" in the code below,
GetSelections() does not provide the correct items.
Is this a bug?
I think so.

Linux 2.6.22.19-0.1-default
Python 2.5.1
wx 2.8.7.1

···

-----------------------------------------------------------------------------------
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

import wx

class ListSelect(wx.Frame):
    def __init__(self, *args, **kwargs):
        self.lst_des = kwargs.pop('lst_des', [])
        self.lst_des_cho = [] # items of lst_des chosen
        wx.Frame.__init__(self, *args, **kwargs)
        self.create_controls()
        self.bind_events()
        self.create_layout()
        self.init_controls()

    def create_controls(self):
        self.panel = wx.Panel(self)
        self.lbx_lst_des = wx.ListBox(self.panel, id=wx.ID_ANY, choices=[], size=(100, 200), style=wx.LB_EXTENDED | wx.LB_SORT)
        # self.lbx_lst_des = wx.ListBox(self.panel, id=wx.ID_ANY, choices=[], size=(100, 200), style=wx.LB_EXTENDED)

    def bind_events(self):
        self.Bind(wx.EVT_LISTBOX, self.on_lbx_lst_des, self.lbx_lst_des)

    def create_layout(self):
        v_des = wx.BoxSizer(wx.VERTICAL)
        v_des.Add(self.lbx_lst_des, 1, wx.EXPAND, 3)
        self.panel.SetAutoLayout(True)
        v_des.SetSizeHints(self)
        self.panel.SetSizer(v_des)
        v_des.Fit(self.panel)
        self.panel.Layout()
        self.Layout()

    def on_lbx_lst_des(self, evt):
        self.lst_des_cho=self.lbx_lst_des.GetSelections()
        print("")
        for item in self.lst_des_cho:
            print("item=%d, string=%s" % (item, self.lst_des[item]))
        evt.Skip()

    def init_controls(self):
        self.lbx_lst_des.Set(self.lst_des)

if __name__ == '__main__':
    app = wx.App()
    lst_des = "a x b y c z".split()
    frame = ListSelect(parent=None, title='list select', lst_des=lst_des)
    frame.Show(True)
    app.MainLoop()
-----------------------------------------------------------------------------------

Grüessli
--
Kurt Mueller, kurt.alfred.mueller@gmail.com

Hello Kurt

Here’s the problem:
You set the listbox items to “lst_des”, asked for it to sort them and expected to get the same item order. Although you explicitly said “self.lbx_lst_des.Set(self.lst_des)”, the wx.LB_SORT option will order the items and change it’s indexes accordingly.

The correct way to do it is changing

        print("item=%d, string=%s" % (item, self.lst_des[item]))

to

        print("item=%d, string=%s" % (item, self.lbx_lst_des.GetString(item)))

This way the item order doesn’t matter and you get the right item no matter what.