[wxPython-users] Passing python object as client data

Jorgen Bodde jorgen.maillist at gmail.com
Mon May 7 00:03:39 PDT 2007


Hi Phil,

Wouldn't it be better to patch this up by overriding this int passing
to the SetItemData to a long? I believe the long <--> object reference
can be done transparently.

If I check the help of wxWidgets C++ API:

wxListCtrl::SetItemData
bool SetItemData(long item, long data)

Thanks again I will reopen the issue by posting a new message asking
for a change in the wxPython binding ..

Regards,
- Jorgen

On 5/7/07, Phil Mayes <phil at philmayes.com> wrote:
> At 12:06 PM 5/6/2007, you wrote:
> >Hi All,
> >
> >I am a bit confused and do not know how to go about doing this..
> >
> >When I add a new item to a wx.Choice or wx.ComboBox I can use
> >SetClientData() and it accepts an arbitrary python object reference
> >like;
> >
> >        """ Tabs need to be added to the list """
> >        # if the tab is the first to be added, select it and fill edit
> >        idx = self.__mTabSelect.Append(message.data.getName())
> >        self.__mTabSelect.SetClientData(idx, message.data)
> >
> >message.data is a Song() object. So that's cool. But when I try to use
> >SetItemData from the wx.ListCtrl, it expects an int:
> >
> >    self.__mLinks.SetItemData(index, l)
> >  File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py",
> > line
> >4561, in SetItemData
> >    return _controls_.ListCtrl_SetItemData(*args, **kwargs)
> >AttributeError: Link instance has no attribute '__int__'
> >
> >With the code:
> >
> >     self.__mLinks.SetItemData(index, l)
> >
> >Where l is a Link() object. I am confused becuase the same code seems
> >to work for the wx.Choice.
> >
> >How do I go about converting the object reference to attach the client
> >data to the list ctrl item? I would like to get a reference back when
> >doubleclicking on the object, so that I do not have to have lookup
> >tables, or use special integer ID's in my object's list..
>
> Yes, this is an annoying limitation in ListCtrl.  I get round it by using a
> dictionary and creating arbitrary integers to map to the objects:
> self.mydict = {}
> number = 0
> for ...:
>      ...
>      self.__mLinks.SetItemData(idx, number)
>      self.mydict[number] = message.data
>      number += 1
> (I don't use idx itself in case the list is being sorted.)
> Phil
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe at lists.wxwidgets.org
> For additional commands, e-mail: wxPython-users-help at lists.wxwidgets.org
>
>




More information about the wxpython-users mailing list