[wxPython-users] dynamic menu with bad callback function

Mel Wilson mwilson at the-wire.com
Sun Jun 4 07:32:23 PDT 2006


oyster wrote:
> I want to create a dyanmic menu accoring to varialbe cfg(in fact I read 
> this
> from a user-defined file)
> cfg=[['a', 'this is a'],['b', 'this is b']]
> where cfg[i][0] is the text of menu, and cfg[i][1] is passed to the 
> callback
> function
> but now, the display are always 'this is b'. how to correct this? thanx
> [code]
[ ... ]

I haven't tested this, but it looks like the i[1] used below 
  will be i[1] as it stands when the menu event actually 
occurs -- not as it stands when you bind the menu event.

>  for i in cfg:
>   wx.EVT_MENU(self,eval('self.IDmnFile%s' % i[0]) , lambda e:self.event
> (i[1]))

Maybe try
     lambda e:self.event (ii=i[1])
to bind the current contents of i[1] with the name ii and 
call your function with that.

As I say, I haven't tested this,

         Good Luck.        Mel.





More information about the wxpython-users mailing list