Tkinter Puzzler

F. Petitjean littlejohn.75 at news.free.fr
Fri Jan 7 06:00:14 EST 2005


Le 07 Jan 2005 05:28:31 EST, Tim Daneliuk a écrit :
> I am trying to initialize a menu in the following manner:
> 
> for entry in [("Up", KeyUpDir), ("Back", KeyBackDir), ("Home", KeyHomeDir), ("Startdir", KeyStartDir), ("Root", 
> KeyRootDir)]:
> 
>      func = entry[1]
>      UI.ShortBtn.menu.add_command(label=entry[0], command=lambda: func(None))
The problem is that you *call* the callback : the command parameter is
bound to the result of func(None)
> 
> However, at runtime, each of the menu options binds to the *last* function
> named in the list (KeyStartDir).
> 
> Explicitly loading each entry on its own line works fine:
> 
> UI........command=lambda:KeyWHATEVERDir(None)
> 
> Any ideas why the first form does not fly?
I would simplify the code like ;

add_cmd = UI.ShortBtn.menu.add_command
for label, func in (("Up", KeyUpDir), .... ):
    add_cmd(label=label, command=func)

And have
def KeyUpDir(arg=None):
    # whatever

> 
> 
> TIA,
> ----------------------------------------------------------------------------
> Tim Daneliuk     tundra at tundraware.com
> PGP Key:         http://www.tundraware.com/PGP/



More information about the Python-list mailing list