Keyboard shortcuts with tkinter in Python 3

I created a menu in Python 3, and I'm wondering how to add keyboard shortcuts and accelerators to it. Like hitting β€œF” for the β€œFile” menu and something else.

After some copying, I found the attribute "underline =", but it does not work in Python 3. It did not 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 this using tkinter in Python 3?

+4
source share
1 answer

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

:

,    . focus_set   :

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()

script, , - .

, ctrl + f :

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

, :

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

ALT, OptionName

= ALT, f = ALT, t ..

,

+4

All Articles