Add shortcut to a Tkinter menu

Asked

Viewed 161 times

1

I wonder how to add a shortcut menu item. I’ve been searching and seen it can be done using a special function common to all widgets in the module tkinker, that is to say the function bind.

What I tried to do was this:

menu_file.add_command(label="Exit", command=exitf, accelerator="Ctrl+Q")
menu_file.bind("<Control-Q>", exitf)

where exitf is a function that closes the application.

But I honestly didn’t understand exactly how this function works bind or the one called bind_all.

What I realized was that the first parameter is the event and the second is Handler, could explain to me better and more in detail how they work or indicate me a good site where I can study this?

1 answer

1

The way I solved this problem was by connecting to Handler function with the event, in my case <Control-Q>, calling the function bind_all of root Tk of my application, and not calling the function bind from the menu directly.

If master is a type object Tk, this is simply an example:

master.bind_all("<Command-q>", quit_application)

For more information on events and bindings in Python see here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.