[Tkinter-discuss] Looking for example of how to use <<MenuSelect>> to determine currently selected menu item
Michael Lange
klappnase at web.de
Sun Nov 21 12:23:27 CET 2010
Hi Malcolm,
Thus spoketh python at bdurham.com
unto us on Sat, 20 Nov 2010 18:09:58 -0500:
> 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.
sure, you're right, I didn't think of that.
>
> 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 god, Tkinter offers a solution which is actually much easier
than my first example :)
########################
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):
print event.widget.index('active')
m.bind('<<MenuSelect>>', callback)
root.mainloop()
#########################
Regards
Michael
.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.
Every living thing wants to survive.
-- Spock, "The Ultimate Computer", stardate 4731.3
More information about the Tkinter-discuss
mailing list