Tkinter and the Menu Widget

Emile van Sebille emile at
Wed Jun 19 09:18:22 EDT 2002

Michele Simionato
>     import Tkinter
>     def dosomething(s):
>         print s
>     root=Tkinter.Tk()
>     mn=Tkinter.Menu(root) #attach the menu mn to root
>     menuD={'File':['File1','File2'],'Help':['Help1','Help2']}
>     smn={} #dictionary keys->submenu-objects defined below
>     for key in menuD.keys():
>         smn[key]=Tkinter.Menu(mn) #define a submenu of mn
>         mn.add_cascade(label=key,menu=smn[key]) #attach smn[key] to mn
>         for submenu in menuD[key]:
>             smn[key].add_command(label=submenu,command=lambda :
>                                  dosomething(key+'='+submenu))

You've got a scope issue.  Try:

        smn[key].add_command(label=submenu,command=lambda key=key,


Emile van Sebille
emile at


More information about the Python-list mailing list