简体   繁体   English

Tkinter Windows 上下文菜单中的快捷方式

[英]Tkinter shortcuts in context menu on Windows

I a have a menu that I display as a context menu in a tkinter application:我有一个菜单,在tkinter应用程序中显示为上下文菜单:

rmenu = tk.Menu(None, tearoff=0, takefocus=0)
# add some commands here
rmenu.add_command(label="Copy", copyfunction, accelerator="Shift-C")
rmenu.bind("<Shift-KeyPress-C>", copyfunction)                       # this doesn't work on windows!

I define a shortcut for a command in this context menu, and then, when I try to run the command by typing <Shift> + <C> on windows it doesn't work.我在此上下文菜单中定义了命令的快捷方式,然后,当我尝试通过在windows上键入<Shift> + <C>来运行命令时,它不起作用。 Actually, I get a beep.实际上,我会听到哔哔声。

Is there a way to define shortcut in this situation?在这种情况下有没有办法定义快捷方式?

Update:更新:

I have even tried to generate a event in the program using:我什至尝试使用以下方法在程序中生成事件:

rmenu.event_generate("<Shift-KeyPress-C>", when="tail")

But the function copyfunction wasn't called.但是没有调用copyfunction复制函数。

PS: This works on Linux PS:这适用于 Linux

bind the keyboard shortcut to the root .将键盘快捷键bindroot Only things with focus listen for key bindings.只有有focus的东西才会监听键绑定。 How often does the context menu have focus?上下文菜单多久获得一次焦点? By using bind_all on the root , whatever has focus will trigger the key binding.通过在root上使用bind_all ,任何有focus的东西都会触发键绑定。

root.bind_all("<Shift-C>", copyfunction)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM