[Tkinter-discuss] How do I identify which menu item has bee selected?

Francesco Bochicchio bieffe62 at gmail.com
Wed Aug 19 10:53:07 CEST 2009

2009/8/18 thicket <mark_eastwood at ntlworld.com>

> Hi - am using python 2.6 and Tkinter 8.5.
> I would like to know how to identify which 'menu item' has been selected
> from a cascaded menu.
> The cascaded menu menu items are variable and created on the fly. The idea
> is to use a common callback for all menu items, the callback function then
> determines which selection was made and runs the appropriate function.
> I have tried binding an event to the selection and then trying to get some
> sort of info from the event object (event.widget()) but this appears to
> return info about the menu class instance and not about it's menu items.
> I've used Java in the past - and if I recall correctly - a menu-item is a
> class, in python it seems to be an attribute(list entry) of the menu class
> -
> in which case - how do I interrogate it if I do not know it's index?
> Any help is appreciated - it's starting to do my head in!!

Hi Mark,

to build a menu you have:

- create a menubar, which is a Menu object f type menubar:
      mbar = Menu( type='menubar', ... )

- create menus, which also are Menu objects, and adding to the menubar by
using the add_cascade method of the menu  bar:
      menu = menu(master=se,f.mbar, ... )
      mbar.add_cascade( menu )

- populate the menu using the add_command method of the menu:
     menu.add_command( label='Do something', command= do_command_callback )

Now, in Tkinter there is no possibility to specify parameters to pass a
callback, but there are several ways to do it, which you can find following
the links from here:  http://tkinter.unpy.net/wiki/Widgets/Button (section
parameter passing: it refers to button callbacks but the same applies for

I personally tend to suggest functools.partial, which you should use like
this to have the same callback called with a differemt menu item:

      menu.add_command( label='Do something',
command= functools.partial(do_command_callback, 1  )
       menu.add_command( label='Do something else',
command= functools.partial(do_command_callback, 2  ) )

In case you need more examples of how to use tk widgets with python, have a
look at this suite of the programs that I wrote some time ago (code is a bit
old but still working with latest python versions):


P.S : in case you don't know, unlike Java, Python has not a 'default widget
set', that you must use if you are coding in python.
Instead, you can use several among the most popular widget toolkits (Gtk+,
Qt, ... ), even Swing if you use the java version of python (Jython), which
is a bit behind as language version (stable supports Python 2.5, beta
supports Python 2.6).
Tkinter has the advantage of being bundled with python (for the others you
have to dounload and install additional modules) and surely is suitable for
many tasks, especially if for you functionality is more important than look
(although with a bit of effort Tk GUI can also look cool, see for instance
aMSN, a clone of mSN Messanger written using the same toolkit of Tkinter,
but not in Python).

Sorry for the verbosity (feeling bored :-), hope I managed to help in the

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20090819/2cce2bc7/attachment.htm>

More information about the Tkinter-discuss mailing list