Change the image of a list item (listctrl)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Change the image of a list item (listctrl)

Donn Ingle
Hello, me again.
I am going blue in the face trying to change the icon of an item in a list
control.

I have an Image list with two icons in it. It's assigned correctly.
The icons are all set in a loop like so:
        for p in pl :
            ipog = Pog ( p )
            try: #catch pogs that are not properly formed
                if ipog.isInstalled ( ): i = 1
                else: i = 0
                li = wx.ListItem  (  )
                li.SetImage  ( i )
                li.SetText  ( p )
                self.InsertItem  ( li )
Nevermind the fluff, the SetImage ( ) part works fine.

So, later, when I select an item and click a button, I want that item's icon
to change.
I am here at the moment, no change actually happens:
        ii = self.GetFocusedItem ( )
        i = self.GetItem ( ii )
        print "PRE:", i.GetImage ( )
        i.SetImage ( 1 )
        print "POST:", i.GetImage ( )

Any help ?

/d



Reply | Threaded
Open this post in threaded view
|

Re: Change the image of a list item (listctrl)

Donn Ingle
Okay - FWIW, I found the SetItemImage command in the wxWidgets docs and that
seems to do the job!

        index = self.GetFocusedItem ( )
        if T.isInstalled ( ): n = 1
        else: n = 0
        self.SetItemImage ( index, n )


/d



Reply | Threaded
Open this post in threaded view
|

Re: Change the image of a list item (listctrl)

Robin Dunn
In reply to this post by Donn Ingle
Donn Ingle wrote:

> Hello, me again.
> I am going blue in the face trying to change the icon of an item in a list
> control.
>
> I have an Image list with two icons in it. It's assigned correctly.
> The icons are all set in a loop like so:
>         for p in pl :
>             ipog = Pog ( p )
>             try: #catch pogs that are not properly formed
>                 if ipog.isInstalled ( ): i = 1
>                 else: i = 0
>                 li = wx.ListItem  (  )
>                 li.SetImage  ( i )
>                 li.SetText  ( p )
>                 self.InsertItem  ( li )
> Nevermind the fluff, the SetImage ( ) part works fine.
>
> So, later, when I select an item and click a button, I want that item's icon
> to change.
> I am here at the moment, no change actually happens:
>         ii = self.GetFocusedItem ( )
>         i = self.GetItem ( ii )
>         print "PRE:", i.GetImage ( )
>         i.SetImage ( 1 )
>         print "POST:", i.GetImage ( )

Add a call to self.SetItem(i).  When you call GetItem it gives you a
copy of the wx.ListItem object not a reference to something used
internally by the listctrl (because it is generated on the fly from the
native list item info) so you need to call SetItem to get that new image
info pushed back to the native widget.

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