Tkinter dropdown Menu with keyboard shortcuts

A dropdown menu is a list of vertically stacked menu items that appears in the top menu bar of an application. We can create a menu bar in a Tkinter application by creating an object of Menu() which contains all the menu items.

Sometimes we want to provide keyboard shortcuts to menu items for faster navigation. To bind keyboard shortcuts to menu items, we use the bind_all(<Key>, callback) method which applies the shortcut globally across the entire application.

Basic Syntax

Here's the basic structure for creating a dropdown menu with keyboard shortcuts ?

# Create menu bar
menubar = Menu(window)

# Create dropdown menu
file_menu = Menu(menubar, tearoff=False)
menubar.add_cascade(label="File", menu=file_menu)

# Add menu item with keyboard shortcut
file_menu.add_command(label="Quit", command=callback, accelerator="Ctrl+Q")

# Bind keyboard shortcut globally
window.bind_all("<Control-q>", callback)

# Configure window to use menu
window.config(menu=menubar)

Complete Example

This example creates a dropdown menu with keyboard shortcuts. Press Ctrl+Q to quit the application ?

import tkinter as tk
from tkinter import messagebox

# Create main window
window = tk.Tk()
window.geometry("600x400")
window.title("Dropdown Menu with Keyboard Shortcuts")

def quit_app(event=None):
    window.destroy()

def show_info(event=None):
    messagebox.showinfo("Info", "This is an information message!")

def show_help(event=None):
    messagebox.showinfo("Help", "Press Ctrl+Q to quit, Ctrl+I for info")

# Create menu bar
menubar = tk.Menu(window)

# Create File menu
file_menu = tk.Menu(menubar, tearoff=False)
menubar.add_cascade(label="File", underline=0, menu=file_menu)
file_menu.add_command(label="Info", underline=0, command=show_info, accelerator="Ctrl+I")
file_menu.add_separator()
file_menu.add_command(label="Quit", underline=0, command=quit_app, accelerator="Ctrl+Q")

# Create Help menu
help_menu = tk.Menu(menubar, tearoff=False)
menubar.add_cascade(label="Help", underline=0, menu=help_menu)
help_menu.add_command(label="About", underline=0, command=show_help, accelerator="F1")

# Configure window to use menu
window.config(menu=menubar)

# Bind keyboard shortcuts globally
window.bind_all("<Control-q>", quit_app)
window.bind_all("<Control-i>", show_info)
window.bind_all("<F1>", show_help)

# Add a label for demonstration
label = tk.Label(window, text="Use keyboard shortcuts:\nCtrl+Q to quit\nCtrl+I for info\nF1 for help", 
                 font=("Arial", 14), justify="center")
label.pack(expand=True)

window.mainloop()

Key Parameters

Parameter Description
tearoff Set to False to prevent menu from being torn off
underline Index of character to underline for Alt+key access
accelerator Text displayed showing the keyboard shortcut
command Function to call when menu item is selected

Common Keyboard Shortcuts

Here are some commonly used keyboard shortcut patterns ?

import tkinter as tk

window = tk.Tk()
window.geometry("500x300")

def placeholder(event=None):
    print("Menu item selected")

menubar = tk.Menu(window)
file_menu = tk.Menu(menubar, tearoff=False)
menubar.add_cascade(label="File", menu=file_menu)

# Common shortcuts
file_menu.add_command(label="New", command=placeholder, accelerator="Ctrl+N")
file_menu.add_command(label="Open", command=placeholder, accelerator="Ctrl+O")
file_menu.add_command(label="Save", command=placeholder, accelerator="Ctrl+S")
file_menu.add_command(label="Copy", command=placeholder, accelerator="Ctrl+C")
file_menu.add_command(label="Paste", command=placeholder, accelerator="Ctrl+V")

window.config(menu=menubar)

# Bind the shortcuts
window.bind_all("<Control-n>", placeholder)
window.bind_all("<Control-o>", placeholder)
window.bind_all("<Control-s>", placeholder)
window.bind_all("<Control-c>", placeholder)
window.bind_all("<Control-v>", placeholder)

window.mainloop()

Conclusion

Dropdown menus with keyboard shortcuts improve user experience by providing quick access to common functions. Use accelerator to display shortcuts and bind_all() to make them functional across the entire application.

Updated on: 2026-03-25T20:39:12+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements