wxpython event question

Thanks for the reply. It is helpful to know that I can compare the button
objects directly with the event triggered button.
I'm just surprised that there isn't a more elegant solution that I know of,
given the elegance of python.

Somehow I'm almost certain there is a better way, is there something like a
event manager that would help with this sort of things? I mean, button
onclick updates an python built-in list must happen all the time. There
must be some mechanism that's just right for this kind of things.

John

===== Original Message From "Mike C. Fletcher" <mcfletch@rogers.com> =====
Following is by no means an elegant solution, but it's the first one
that pops into my mind...

shuoyang wrote:

Hi dear wxPython users:

I am pretty new to wxPython, and find it amazing, not to mention powerful!
But I've run into one problem, I've read thru the tutorials, but can't seem

to

figure out how to do this:

#I have a list of things:
MyList = [elem1,elem2,....]

itemMapping = # MyList[i] is represented by control with ID
itemMapping[i]

#then I have loop:
for v in MyList:
make static text with v
make a button to delete v from MyList, call OnClick

itemMapping.append( (button,text) )

def OnClick(self,event)
need to know the index of v to delete v from MyList, but how?

for i, (object,(button,text)) in enumerate( zip(MyList, itemMapping)):
   if button == event.GetEventObject(): # might be GetId(), been a while :slight_smile:
      del MyList[i]
      # remove controls from parent here
      del itemMapping[i]
      break # all further indices are messed up anyway...

Since python list is not object in wxPython, i don't think the event

parameter

···

in OnClick would help. Can anyone offer a hint? Thanks!
John

In real-world code I'd likely make the model-view split a little more
elegant and less fragile, but then I'm somewhat obsessive about those
kinds of things.

HTH,
Mike

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

shuoyang wrote:

Thanks for the reply. It is helpful to know that I can compare the button
objects directly with the event triggered button.
I'm just surprised that there isn't a more elegant solution that I know of,
given the elegance of python.

As a rule, I find that wxPython, being bindings around C++, is not as elegant as Python intself. However, I'm not sure this is the case here. This can be made more elegant (IMHO).

Somehow I'm almost certain there is a better way, is there something like a
event manager that would help with this sort of things? I mean, button
onclick updates an python built-in list must happen all the time.

Probably, but button OnClick updates SOMETHING is probably the general rule, having something specific to lists would be too special purpose. Being able to pass an extra piece of data to the bound mthod would be handy...can that be done?

What you need to do is keep an object around that tells you which button corresponds to what. This is what comes to mind:

MyList = [elem1,elem2,....]

Create a dict for the mapping:
self.mapping = {}

#then I have loop:

  for v in MyList:

  make static text with v
  make a button to delete v from MyList, call OnClick

     self.mapping[button.GetId()] = v

def OnClick(self,event)
  need to know the index of v to delete v from MyList, but how?

     MyList.remove(self.mapping[event.GetId()])

This is similar to Mike's suggestion, but the dict keeps you from having to search the list in every OnClick

Note: this will only work if your list elements are unique. If not, you'll want to store the element indices instead, except that they will change with each removal.

You have both a static text and a button for each element. When you click the button, v gets deleted, so you'll have to re-build all your static texts and buttons. It just seems odd. What's the bigger picture here? This seems a little clunky.

What are you really trying to do?

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

shuoyang wrote:

Thanks for the reply. It is helpful to know that I can compare the button
objects directly with the event triggered button.
I'm just surprised that there isn't a more elegant solution that I know of,
given the elegance of python.

Somehow I'm almost certain there is a better way, is there something like a
event manager that would help with this sort of things? I mean, button
onclick updates an python built-in list must happen all the time. There
must be some mechanism that's just right for this kind of things.

There are several ways to do it, wxPython doesn't try to stipulate the method. For example you could create a dictionary that maps the ID of the button to a function to the flag you want to set, and the EVT_BUTTON handler can just use event.GetId() to do the right thing.

···

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