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

thicket mark_eastwood at ntlworld.com
Wed Aug 19 14:42:02 CEST 2009

Thanks Francesco, Guilherme -

you've both given me plenty to experiment with (and learn :-)). I have a
'partial' solution to what I'm trying to achieve (code snippet below) 
                self.menubar = Menu(self.frm)

                # create the variable 'Action' sub-menu list
                # each sub-menu entry will have an (on)value which
corresponds to it's DB tablename (self.tables[value - 1])
                self.mnth_mu=Menu(self.menubar, tearoff=0)

                # Strip out the month_yr part of the DB tablename (e.g.
04_April_09) and create a radiobutton entry on the
                # menu for each table that currently exists - a table is
only created at the start of a month
                for mnth in self.tables:
variable=self.muVar, value=self.on.get(), command=self.alt_cmd)

                # create the 'Action' menu list
                self.act_mu = Menu(self.menubar, tearoff=0)
                self.act_mu.add_command(label="Low stock - closing less
monthly sales", command=self.low_stock)
                self.act_mu.add_command(label="Profit and Loss",
                self.act_mu.add_cascade(label="Stock/Order History",
                self.act_mu.add_cascade(label="Top two sellers by quantity",
                self.act_mu.add_cascade(label="Top two earners",
                self.act_mu.add_cascade(label="Bottom two sellers by
quantity", menu=self.mnth_mu)
                self.act_mu.add_cascade(label="Bottom two earners",
                self.menubar.add_cascade(label="Actions", menu=self.act_mu)

                # display the menu
               def alt_cmd(self):
                    # Determine the selection, match it to the correct
tablename then call appropriate function to generate the sql
                    print self.muVar.get()
The issue is that all the 'Actions' after and including Stock/Order history
will display the same sub-menu (self.mnth_mu).

The code as it stands enables me to identify which month has been selected
for a particular 'Action' but not which 'Action' it refers to i.e. works
fine if self.mnth.mu is used only once.

A solution is to create a separate menu for each 'Action' - I am averse to
doing this because the sub-menu for each action that requires a sub-menu is
identical and yes I'm a bit of a lazy programmer! 

I was hoping to - whatever the level of a menu - when a selection is made -
to be able to identify the 'Action' and the 'Month' selected.

I hope this makes sense ... I shall look into what you both have said
View this message in context: http://www.nabble.com/How-do-I-identify-which-menu-item-has-bee-selected--tp25023156p25043302.html
Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.

More information about the Tkinter-discuss mailing list