how to sort text in wx.ListCtrl

Hiya All,

I’ve had a look at the wxPython2.8 demos where you have class name ListCtrComboPopup derived from wx.ListCtrl and wx.Combo.ComboPopup. In that class a function AddItem is defined which you can use to add text to the list. I would like to add more text to the list, but I would like to have it be sorted automatically. But how do I get that ?

class ListCtrlComboPopup(wx.ListCtrl, wx.combo.ComboPopup):
def init(self, log=None):
self.PostCreate(wx.PreListCtrl())
wx.combo.ComboPopup.init(self)

def AddItem(self, txt):
self.InsertStringItem( self.GetItemCount(), txt )

Robert,

Hiya All,
I've had a look at the wxPython2.8 demos where you have class name ListCtrComboPopup derived from wx.ListCtrl and wx.Combo.ComboPopup. In that class a function AddItem is defined which you can use to add text to the list. I would like to add more text to the list, but I would like to have it be sorted automatically. But how do I get that ?
class ListCtrlComboPopup(wx.ListCtrl, wx.combo.ComboPopup):
    def __init__(self, log=None):
        self.PostCreate(wx.PreListCtrl())
        wx.combo.ComboPopup.__init__(self)
    def AddItem(self, txt):
        self.InsertStringItem( self.GetItemCount(), txt )

If I were you, I'd just keep a reference to the list you put into the listctrl to begin with. Then when you add an item, do something like this:

<code>

# untested code
myList = [1,3,4]

def onAddItem():
    myList.append(2)
    myList.sort()
    myListCtrlPopup.SetItems(myList)

</code>

Didn't I just tell someone on here to do that yesterday? Hmmm...

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Robert Robert wrote:

Hiya All,
I've had a look at the wxPython2.8 demos where you have class name ListCtrComboPopup derived from wx.ListCtrl and wx.Combo.ComboPopup. In that class a function AddItem is defined which you can use to add text to the list. I would like to add more text to the list, but I would like to have it be sorted automatically. But how do I get that ?

Either use the wx.ListCtrl's ability to sort the items itself, or insert the new item into the list ctrl at the proper position to maintain the sort.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

I’ve had a look at it, but there is no function where I can just give a reference of the list. So no “SetItems” function at all. Or any other function with that name. Any other suggestion ?

···

— On Fri, 10/24/08, Mike Driscoll mdriscoll@co.marshall.ia.us wrote:

From: Mike Driscoll mdriscoll@co.marshall.ia.us
Subject: Re: [wxpython-users] how to sort text in wx.ListCtrl
To: wxpython-users@lists.wxwidgets.org
Date: Friday, October 24, 2008, 7:37 PM


Robert,
> Hiya All,
> I've had a look at the wxPython2.8 demos where you have class name > ListCtrComboPopup derived from wx.ListCtrl and wx.Combo.ComboPopup. In > that class a function AddItem is defined which you can use to add text > to the list. I would like to add more text to the list, but I would > like to have it be sorted automatically. But how do I get that ?
> > class ListCtrlComboPopup(wx.ListCtrl, wx.combo.ComboPopup):
> def __init__(self, log=None):
> self.PostCreate(wx.PreListCtrl())
> wx.combo.ComboPopup.__init__(self)
> def AddItem(self, txt):
> self.InsertStringItem( self.GetItemCount(), txt )
>
>
If I were you, I'd just keep a reference to the list you put into the listctrl to begin with. Then when you add an item, do something like this:
<code>
# untested code
myList = [1,3,4]
def onAddItem():
myList.append(2)
myList.sort()
myListCtrlPopup.SetItems(myList)
</code>
Didn't I just tell someone on here to do that yesterday? Hmmm...
-------------------
Mike Driscoll
Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

I did try the SortItems method in which you need to pass as argument your own sort function. I am kind of confused. When I look at my code I have an idea that the “InsertStringItem” function takes an index and that my item is a text string. My sort function looks like this:

def mySorter( x, y ):

if x < y:

return y

return x

Then whenever I add a new text I call the SortItems( mySorter ). But when I monitor what x, y is in my mySorter function I get only x =0, y =0 or type integers. I don’t understand this at all. Are my items indexes ? and the text string the value it contains ? If that is the case, I want to sort my text string and not the indexes.

Any suggestion on how to get this done ?

···

— On Fri, 10/24/08, Robin Dunn robin@alldunn.com wrote:

From: Robin Dunn robin@alldunn.com
Subject: Re: [wxpython-users] how to sort text in wx.ListCtrl
To: robertrobert93@yahoo.com, wxpython-users@lists.wxwidgets.org
Date: Friday, October 24, 2008, 11:30 PM


Robert Robert wrote:
> Hiya All,
> I've had a look at the wxPython2.8 demos where you have class name > ListCtrComboPopup derived from wx.ListCtrl and wx.Combo.ComboPopup. In > that class a function AddItem is defined which you can use to add text > to the list. I would like to add more text to the list, but I would like > to have it be sorted automatically. But how do I get that ?
Either use the wx.ListCtrl's ability to sort the items itself, or insert the new item into the list ctrl at the proper position to maintain the sort.
-- Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robert Robert wrote:

I've had a look at it, but there is no function where I can just give a reference of the list. So no "SetItems" function at all. Or any other function with that name. Any other suggestion ?

Since I haven't used that widget, I'm not sure what to tell you. Hopefully someone who has used it will have a clue and let us in on the secret.

Mike

···

--- On *Fri, 10/24/08, Mike Driscoll /<mdriscoll@co.marshall.ia.us>/* > wrote:

    From: Mike Driscoll <mdriscoll@co.marshall.ia.us>
    Subject: Re: [wxpython-users] how to sort text in wx.ListCtrl
    To: wxpython-users@lists.wxwidgets.org
    Date: Friday, October 24, 2008, 7:37 PM

    Robert,

    > Hiya All,
    > I've had a look at the wxPython2.8 demos where you have class name > ListCtrComboPopup derived from wx.ListCtrl and wx.Combo.ComboPopup. In > that class a function AddItem is defined which you can use to add text > to the list. I would like to add more text to the list, but I would > like to have it be sorted automatically. But how do I get that ?
    > > class ListCtrlComboPopup(wx.ListCtrl, wx.combo.ComboPopup):
    > def __init__(self, log=None):
    > self.PostCreate(wx.PreListCtrl())
    > wx.combo.ComboPopup.__init__(self)
    > def AddItem(self, txt):
    > self.InsertStringItem( self.GetItemCount(), txt )
    >

    If I were you, I'd just keep a reference to the list you put into the listctrl to begin with. Then when you add an item, do something like this:

    <code>

    # untested code
    myList = [1,3,4]

    def onAddItem():
        myList.append(2)
        myList.sort()
        myListCtrlPopup.SetItems(myList)

    </code>

    Didn't I just tell someone on here to do that yesterday? Hmmm...

    -------------------
    Mike Driscoll

    Blog: http://blog.pythonlibrary.org
    Python Extension Building Network: http://www.pythonlibrary.org

    ______________________