Tkinter and the Menu Widget

Emile van Sebille emile at fenx.com
Wed Jun 19 15:18:22 CEST 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,
submenu=submenu:
                             dosomething(key+'='+submenu))

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list