Search code examples
pythonpython-3.xtkintermenukeyboard-shortcuts

Keyboard shortcuts with tkinter in Python 3


I've created a menubar in Python 3, and I'm wondering how to add keyboard shortcuts and accelerators to it. Like hitting "F" for File Menu and whatnot.

Through some digging around I found the "underline=" attribute, but it doesn't seem to work in Python 3. It didn't work when I tried it, and the only documentation I found for it was for earlier versions of Python.

    menubar = Menu(master)

    filemenu = Menu(menubar, tearoff=0)
    .....
    menubar.add_cascade(label="File", underline=0, menu=filemenu)

Is there a way to do these things with tkinter in Python 3?


Solution

  • consider reading (http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm)

    you have to bind your widget to an event to your function:

    Keyboard events are sent to the widget that currently owns the keyboard focus. You can use the focus_set method to move focus to a widget:

    Capturing keyboard events

    from Tkinter import *
    
    root = Tk()
    
    def key(event):
        print "pressed", repr(event.char)
    
    def callback(event):
        frame.focus_set()
        print "clicked at", event.x, event.y
    
    frame = Frame(root, width=100, height=100)
    frame.bind("<Key>", key)
    frame.bind("<Button-1>", callback)
    frame.pack()
    
    root.mainloop()
    

    If you run this script, you’ll find that you have to click in the frame before it starts receiving any keyboard events.

    I followed this guide to implement a ctrl+f binding to one of my functions a while ago:

    toolmenu.add_command(label="Search Ctrl+f", command=self.cntrlf)
    root.bind('<Control-f>', self.searchbox)
    def cntrlf(self, event):
        self.searchbox()
    

    for your file menu, you might want to consider implementing accelerators:

    menubar.add_cascade(label="File", menu=fileMenu)
    fileMenu.add_command(label="Exit", command=quit, accelerator="Ctrl+Q")
    config(menu=menubar) 
    

    for Menu options remember to use ALT followed by the first letter of the OptionName

    file Menu = ALT followed by f Tool Menu = ALT followed by t and so on

    hope this provides usefull