我在tkinter中有一个文件菜单,当我点击它时,一个文件菜单打开。我还希望菜单以键盘快捷键打开,例如“alt+f”,而不是单击它
代码如下:
def Open_FileMenu_With_KeyboardShortcut():
pass
# How would I make the file menu appear when I click "Alt+f"
root.bind("<the code for alt-f>", Open_FileMenu_With_KeyboardShortcut)
# File Option for Menu Bar
FileOption = Menu(MenuBar, tearoff=False)
MenuBar.add_cascade(label="File", menu=FileOption, underline=0)
FileOption.config(bg="White", fg="Black", activebackground="Whitesmoke", activeforeground="Black", activeborderwidth=1, font=('Monaco', 11))
# New Option for File Option
NewMenu = Menu(FileOption, tearoff=False)
NewMenu.config(bg="White", fg="Black", activebackground="Whitesmoke", activeforeground="Black", activeborderwidth=1, font=('Monaco', 11))
NewMenu.add_command(label="New File", command=NewFile)
NewMenu.add_command(label="From Template", command=None)
# Cascade the New menu to the File Menu
FileOption.add_cascade(label="New", menu=NewMenu)
# The remaining settings options
FileOption.add_command(label="Open File", command=OpenFile, accelerator="Ctrl+O")
FileOption.add_command(label="Open Folder", command=None, accelerator="Ctrl+Shift+O")
FileOption.add_command(label="Open Recent", command=None)
FileOption.add_separator()
FileOption.add_command(label="Save File", command=SaveFile, accelerator="Ctrl+S")
FileOption.add_command(label="Save As", command=SaveFileAs, accelerator="Ctrl+Shift+S")
FileOption.add_separator()
FileOption.add_command(label="Revert File", commmand=None)
FileOption.add_command(label="Close Editor", command=None, accelerator="Ctrl+W")
FileOption.add_separator()
FileOption.add_command(label="Quit", command=QuitApplication, accelerator="Ctrl+q")
如何使用键盘快捷键打开“文件”菜单
我不能100%确定问题是什么,但从我对问题的理解来看,这应该是可行的:
同时按
Alt
和f
将打开文件菜单将命令绑定到root时,您只需要<;Alt键->;还有你想用的钥匙,在你的例子中是f
root.bind("<Alt-key-f>", Open_FileMenu_With_KeyboardShortcut)
相关问题 更多 >
编程相关推荐