Catching wx events

Cruelemort ian.inglis at
Thu Jan 18 15:12:17 CET 2007

Hello all,

I am new to this group (and new to Python) and was hoping someone would
be able to help me with something, it is not so much a problem it is
more of a general interest query about something i have a solution too
but am not sure it is the correct one.

I have a class that contains a string ID and a name, and a list
containing a few objects of this type, i need to loop through this list
and create a button for each object (with the name as the label) i have
done this with the following code -

                for chan in self._channellist:
                        channelbutton = wx.Button(self, id=-1,


My question is this - in the _channelChanged method, how do i know
which button has been pressed when i enter the channel changed method,
and so how do i retrieve the appropriate object depending on which
button has been pressed?

I have potentially solved this problem using the UserData property in a
sizer, i have added all the buttons to a sizer for display purposes and
so SizerItem objects have been created, i can then set the original
object to the UserData object by putting the following line of code in
the loop

                        sizeritem =
self.topsizer.Add(channelbutton,0,wx.ALIGN_RIGHT, userData=chan)

This way i can retrieve the item in the _channelChanged method with the
following -

        def _channelChanged(self, event):
                eventobj = event.GetEventObject()
                chan = self.topsizer.GetItem(eventobj).GetUserData()

This works fine but by looking at the API it would appear the UserData
property is not really designed for this use ("userData - Allows an
extra object to be attached to the sizer item, for use in derived
classes when sizing information is more complex than the proportion and
flag will allow for").

Another option would be to derive my own Button class and include the
object in there.

Any advice on the best way to solve this problem would be appreciated.

Many thanks


More information about the Python-list mailing list