dictionary - Python Tkinter Menu Command Not Working -



dictionary - Python Tkinter Menu Command Not Working -

i trying execute next code in python 2.6.5. want show main window 'applications' menu. want menu have series of commands should correspond keys of apps dictionary. when click command, default web browser open , navigate url in apps dictionary particular key. instead, when execute code browser opening first url in apps dictionary without clicking. help please!

from tkinter import * import webbrowser #real links pages on intranet. apps={ 'google':'http://www.google.com/', 'yahoo':'http://www.yahoo.com/' } def openapp(appurl): webbrowser.open(appurl, new=1, autoraise=1) homecoming none root=tk() menubar=menu(root) root.config(menu=menubar) appsmenu=menu(menubar) app in apps: appsmenu.add_command(label=app, command=openapp(apps[app])) menubar.add_cascade(label='apps', menu=appsmenu) root.mainloop()

appsmenu.add_command(label=app, command=openapp(apps[app]))

command parameters phone call functions need wrapped in lambda, prevent them beingness called right away. additionally, commands bound within loop need looping variable default argument, in order bind right value each time.

appsmenu.add_command(label=app, command=lambda app=app: openapp(apps[app]))

python dictionary tkinter command

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -