本文介绍了如何分配热键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助为顶部条目菜单打开"分配热键.
please help to assign hotkeys to the top entry menu "OPEN".
import tkinter
def fileOpen():
print('qwerty')
def makeMenu(parent):
top = tkinter.Menu(parent)
parent.config(menu = top)
file = tkinter.Menu(top, tearoff = False)
file.add_command(label = 'Open...', command = fileOpen, accelerator = 'ctrl+o')
top.add_cascade(label = 'File', menu = file)
#top.bind_class(top, '<Control-Key-o>', fileOpen)
root = tkinter.Tk()
makeMenu(root)
root.mainloop()
我需要按CTRL + O"运行函数fileOpen"
I need to by pressing "CTRL + O" runs the function "fileOpen"
推荐答案
您需要:
将
fileOpen
绑定到根窗口 (root
) 而不是菜单栏 (top
).
Bind
fileOpen
to the root window (root
) instead of the menubar (top
).
使 fileOpen
接受一个参数,当您按下 + 时,该参数将被发送给它.
Make fileOpen
accept an argument, which will be sent to it when you press +.
以下是解决这些问题的脚本版本:
Below is a version of your script that addresses these issues:
import tkinter
######################
def fileOpen(event):
######################
print('qwerty')
def makeMenu(parent):
top = tkinter.Menu(parent)
parent.config(menu = top)
file = tkinter.Menu(top, tearoff = False)
file.add_command(label = 'Open...', command = fileOpen, accelerator = 'ctrl+o')
top.add_cascade(label = 'File', menu = file)
root = tkinter.Tk()
############################################
root.bind_all('<Control-Key-o>', fileOpen)
############################################
makeMenu(root)
root.mainloop()
我更改的内容在评论框中.
The stuff I changed is in comment boxes.
这篇关于如何分配热键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!