Reputation: 315
I have a file menu in tkinter and when I click on it a file menu opens. I also want the menu to open with a keyboard short-cut like "alt+f" for example instead of clicking it.
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?
Upvotes: 4
Views: 1215
Reputation: 41
I know this post is a few years old but I found this thread while looking for an answer and found a hack that works for my purposes.
# Function to instantiate an instance of your menu widget
def access_menu(event):
# Get the x and y coordinates of your widget
x = root.winfo_x()
y = root.winfo_y()
# The post method instantiates an instance of the menu widget you call it on
FileOption.post(x+8, y+30) # Your x and y values to fit
#Bind the command to the root window so it can be called at any time and choose your keybind.
#In this example I chose Escape.
root.bind('<Escape>', access_menu)
This isn't the most elegant solution, but with no built in way to change the keybind to focus the menu it works for what I need. Hopefully it can help someone else out.
Upvotes: 0
Reputation: 21
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.bind("<Alt-key-f>", Open_FileMenu_With_KeyboardShortcut)
Upvotes: 0
Reputation: 7680
I am not 100% sure what the question is asking but from my understanding of the question, this should work:
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.
Upvotes: 4