Currently I'm working on a little tool (kind of a Mapeditor) that makes use
of the ListCtrl in wx.LC_ICON mode. I dynamically add ImageStringItems, also
I add the images to the according ImageList dynamically.
This works perfect so far!
My problem now is to get information about the current item in the ListCtrl.
When I have a selection and respond to a Doubleclick
(wx.EVT_LIST_ITEM_ACTIVATED), I should get information about the item.
The docs say, I should get the id as well as the Image, but the id always
returns 0 and image is always -1.
Does anybody know, how I can find out which image belongs to which item (and
as well get the other information).
Maybe I completely missing something.
Currently I'm working on a little tool (kind of a Mapeditor) that makes use
of the ListCtrl in wx.LC_ICON mode. I dynamically add ImageStringItems, also
I add the images to the according ImageList dynamically.
This works perfect so far!
My problem now is to get information about the current item in the ListCtrl.
When I have a selection and respond to a Doubleclick
(wx.EVT_LIST_ITEM_ACTIVATED), I should get information about the item.
The docs say, I should get the id as well as the Image, but the id always
returns 0 and image is always -1.
Does anybody know, how I can find out which image belongs to which item (and
as well get the other information).
Maybe I completely missing something.
1) The ImageList must be set to contain the size of the images you
want in it. Your example asks for a 32x32 image but the image list is
defined as 64x64. So adding a 32x32px image fails.
2) If you keeping a reference to the image list yourself (i.e
self.iList) then its better to use 'self.SetImageList' instead of
AssignImageList, as AssignImage list assigns the management of the
ImageList object to the window its called on where as SetImageList
tells it just to use the ImageList but not take ownership.
After correcting the above the code worked as expected on my computer
and the activate handler printed the correct values for the
Text/Id/Image Id.
What version of wxPython and what Platform are you using?
Cody
···
On Mon, Feb 21, 2011 at 9:07 AM, Peter R <perum@gmx.net> wrote:
Here are the requested platform information:
Windows XP SP 3 32 bit, Python 2.6.6, wxPython 2.8.11
1) Didn't see, that I put in a wrong information into the dialog title.
The funny thing is, that Windows is able to take and display images as long
as they are => 64x64 pixel. This is a nice side effect and I often use
bigger images with the ListCtrl.
2) I changed the code to SetImageList, but the result is still the same. I
get the item name, but not the id nor the image-id.
Example:
T = event.GetItem()
print type(t) --> <class 'wx._controls.ListItem'>
print t.GetText() --> edit.png
print t.GetId() --> 0
print t.GetImage() --> -1
I guess, I could also use some kind of HitTest() to get the informatio, but
I tought, this way would be more elegant and correct?
Thanks,
Peter
···
-----Ursprüngliche Nachricht-----
Von: wxpython-users@googlegroups.com [mailto:wxpython-
users@googlegroups.com] Im Auftrag von Cody Precord
Gesendet: Montag, 21. Februar 2011 16:29
An: wxpython-users@googlegroups.com
Betreff: Re: [wxPython-users] wx.ListCtrl: Item Information on Double
Click
Hi,
On Mon, Feb 21, 2011 at 9:07 AM, Peter R <perum@gmx.net> wrote:
> Hello everybody!
>
> Currently I'm working on a little tool (kind of a Mapeditor) that
makes use
> of the ListCtrl in wx.LC_ICON mode. I dynamically add
ImageStringItems, also
> I add the images to the according ImageList dynamically.
> This works perfect so far!
>
> My problem now is to get information about the current item in the
ListCtrl.
> When I have a selection and respond to a Doubleclick
> (wx.EVT_LIST_ITEM_ACTIVATED), I should get information about the
item.
> The docs say, I should get the id as well as the Image, but the id
always
> returns 0 and image is always -1.
>
> Does anybody know, how I can find out which image belongs to which
item (and
> as well get the other information).
> Maybe I completely missing something.
>
> Here's a stripped down (but working) version with inspection enabled:
> http://paste.pocoo.org/show/342295/
>
>
> Any help would be highly appreciated. Thanks!
There are some issues with the sample code
1) The ImageList must be set to contain the size of the images you
want in it. Your example asks for a 32x32 image but the image list is
defined as 64x64. So adding a 32x32px image fails.
2) If you keeping a reference to the image list yourself (i.e
self.iList) then its better to use 'self.SetImageList' instead of
AssignImageList, as AssignImage list assigns the management of the
ImageList object to the window its called on where as SetImageList
tells it just to use the ImageList but not take ownership.
After correcting the above the code worked as expected on my computer
and the activate handler printed the correct values for the
Text/Id/Image Id.
What version of wxPython and what Platform are you using?
If the activation is for the first item in the listctrl then the zero for GetId is correct.
It looks like the listitem object assigned to the event object did not use a mask that filled all of the attributes, you can use GetMask to double-check that if you like. Since the wxPython wrapper sets all bits in the mask you can work around this by fetching the item object from the listctrl yourself, something like this:
Here are the requested platform information:
Windows XP SP 3 32 bit, Python 2.6.6, wxPython 2.8.11
1) Didn't see, that I put in a wrong information into the dialog title.
The funny thing is, that Windows is able to take and display images as long
as they are => 64x64 pixel. This is a nice side effect and I often use
bigger images with the ListCtrl.
2) I changed the code to SetImageList, but the result is still the same. I
get the item name, but not the id nor the image-id.
Example:
T = event.GetItem()
print type(t) --> <class 'wx._controls.ListItem'>
print t.GetText() --> edit.png
print t.GetId() --> 0
print t.GetImage() --> -1
I guess, I could also use some kind of HitTest() to get the informatio, but
I tought, this way would be more elegant and correct?
Thank you. The zero for GetId is/was on every item.
Your hint with "self.list.GetItem(event.GetIndex())" did the trick.
Now it works and I get the item text, item id, and most important for me:
the Image ID.
Thank you all for your help!
Regards,
Peter
···
If the activation is for the first item in the listctrl then the zero
for GetId is correct.
It looks like the listitem object assigned to the event object did not
use a mask that filled all of the attributes, you can use GetMask to
double-check that if you like. Since the wxPython wrapper sets all
bits
in the mask you can work around this by fetching the item object from
the listctrl yourself, something like this: