简体   繁体   English

如何使用键盘快捷键/绑定激活 tkinter 菜单和工具栏?

[英]How to activate tkinter menu and toolbar with keyboard shortcut/binding?

I have a file menu in tkinter and when I click on it a file menu opens.我在 tkinter 中有一个文件菜单,当我单击它时会打开一个文件菜单。 I also want the menu to open with a keyboard short-cut like "alt+f" for example instead of clicking it.我还希望菜单使用诸如“alt+f”之类的键盘快捷键打开,而不是单击它。

Here is the code:这是代码:

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")

How would I open the file menu with the keyboard shortcut?如何使用键盘快捷键打开文件菜单?

I am not 100% sure what the question is asking but from my understanding of the question, this should work:我不是 100% 确定问题在问什么,但根据我对问题的理解,这应该有效:

import tkinter as tk

root = tk.Tk()

MenuBar = tk.Menu(root, tearoff=False) # Create the main menu
root.config(menu=MenuBar) # Assign it to the root

# File Option for Menu Bar 
FileOption = tk.Menu(MenuBar, tearoff=False)
MenuBar.add_cascade(label="File", menu=FileOption, underline=0)
...
FileOption.add_command(label="Quit", command=exit, accelerator="Ctrl+q")

Pressing Alt and f at the same time will open the file menu.同时按Altf将打开文件菜单。

When binding the command to root all you need is <Alt-key-> along with the key you want to use, in your case f.将命令绑定到 root 时,您只需要 <Alt-key-> 以及要使用的密钥,在您的情况下为 f。

root.bind("<Alt-key-f>", Open_FileMenu_With_KeyboardShortcut)

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

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