Tkinter: passing parameters to menu commands

Philippe C. Martin philippecmartin at
Fri Jan 7 09:18:24 EST 2005

>>menu.add_cascade(label="File", menu=filemenu)
>>filemenu.add_command(label="New", command=lambda: callback('New'))
>>filemenu.add_command(label="Open...", command=lambda:
>>filemenu.add_command(label="Exit", command=lambda: callback('Exit'))
>>Of course you could do this with named forwarding functions if you

I'm not sure what 'named forwarding functions' are but I'm actually in a
class and when applying your suggestion in the following manner,
everything works (THANKS!)

def __Dec(self,p_string):
  for i in p_string:
#menu creation
l_dec.add_command(label = 'ATR', command=lambda: self.__Dec('ATR'))
l_dec.add_command(label = 'IN', command=lambda:self.__Dec('IN'))

Yet I have a question:

If I replace the menu creation code as below, and since __Insert appends
the string p_string into a text widget that is created _after_ the menu
creation; the method __Dec seems to be called at the menu creation and
I get an error in __Insert because the test widget is equal to None.

My reflexes of C programmer tell me that command=self.__Dec.... just
passes a method pointer (sorry I said it) to add_command - yet it does
not seem to be so.

What is actually going on ?

#menu creation
l_dec.add_command(label = 'ATR', command=self.__Dec('ATR'))
l_dec.add_command(label = 'IN', command=self.__Dec('IN'))



Philippe C. Martin
SnakeCard LLC

More information about the Python-list mailing list