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:
捕获键盘事件
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)
frame.bind("", 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.
我按照本指南实现了一个ctrl f绑定到我之前的一个函数:
toolmenu.add_command(label="Search Ctrl+f", command=self.cntrlf)
root.bind('', 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的第一个字母
file Menu = ALT后跟f
工具菜单= ALT后跟t
等等
希望这提供有用的