[Tkinter-discuss] Looking for example of how to use <<MenuSelect>> to determine currently selected menu item

python at bdurham.com python at bdurham.com
Sun Nov 21 00:09:58 CET 2010


Hi Michael,

Thank you for your idea to use the <Motion> event. That works when I use
the mouse, but it doesn't work when a user uses the cursor keys to move
between menu items.

I can trap the <<MenuSelect>> event - I just need a way to determine the
current menu widget and its active index.

The event object passed to the function I bind to has event.widget
reference to a string vs. a widget. Do you have any ideas on how I can
determine the active menu widget and its active index independent of a
current event object?

Thank you,
Malcolm


----- Original message -----
From: "Michael Lange" <klappnase at web.de>
To: tkinter-discuss at python.org
Date: Sat, 20 Nov 2010 00:11:42 +0100
Subject: Re: [Tkinter-discuss] Looking for example of how to use
<<MenuSelect>> to determine currently selected menu item

Hi,

Thus spoketh python at bdurham.com 
unto us on Fri, 19 Nov 2010 17:25:38 -0500:

> I'm looking for an example of how to use <<MenuSelect>> to
> determine currently selected menu item.
> 
> Use case: Display a context sensitive help message in an
> application's statusbar as a user navigates through various menu
> items.
> 

Maybe you are better off with a simple Motion event, you can access the
menu item under the mouse pointer through the menu's index() method
(from the menu manpage:

Many of the widget commands for a menu take as one argument an indicator
of which entry of the menu to operate on. These indicators are called
indexes and may be specified in any of the following forms:
(...)
@number
    In this form, number is treated as a y-coordinate in the menu's
    window; the entry closest to that y-coordinate is used. For example,
    ``@0'' indicates the top-most entry in the window.

)
So, in this simple example, you will surely see the point:

################################

from Tkinter import *

root = Tk()
m = Menu(root)
m.add_command(label='foo')
m.add_command(label='bar')
m.add_command(label='blah')

root.bind('<3>', lambda event: m.tk_popup(event.x_root, event.y_root))

def callback(event):
    try:
        print m.entrycget(m.index('@%d' % event.y), 'label')
    except TclError:
        print 'this must have been the tearoff or something'

m.bind('<Motion>', callback)

root.mainloop()

################################



I hope this helps

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. .
.-.

Extreme feminine beauty is always disturbing.
		-- Spock, "The Cloud Minders", stardate 5818.4
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss



More information about the Tkinter-discuss mailing list