Add shortcut to a menu in tkinter

1

I'd like to know how to add a shortcut to a menu item. I already had to search and I saw that it can be done using a special function common to all widgets of the tkinker module, that is the bind function.

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 did not see exactly how this bind function works or the other one called bind_all .

What I realized was that the first parameter is the event and the second is the handler, could you explain me better and more in detail how they work or tell me a good site where I can study it?

    
asked by anonymous 19.10.2014 / 17:18

1 answer

1

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

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

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

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

    
10.01.2015 / 03:23