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
等等
希望这提供有用的
这篇博客介绍了Tkinter库中如何处理键盘事件,重点在于焦点管理。通过`focus_set`方法可以转移焦点到特定的widget,使得该widget能够接收键盘输入。作者展示了如何绑定键盘事件和点击事件,并给出了一个示例,其中需要先点击frame才能开始接收键盘事件。此外,还提到了菜单项的快捷键设置,如使用`accelerator`参数创建'Ctrl+Q'的退出快捷方式。博客还提醒读者为菜单选项设置ALT快捷键,以便更高效地操作。
612

被折叠的 条评论
为什么被折叠?



