August 2018
Intermediate to advanced
366 pages
10h 14m
English
The tkinter.Menu class allows us to create menus, submenus, actions, and separators. So, it provides everything we might need to create basic menus in our GUI-based application:
import tkinter
def set_menu(window, choices):
menubar = tkinter.Menu(root)
window.config(menu=menubar)
def _set_choices(menu, choices):
for label, command in choices.items():
if isinstance(command, dict):
# Submenu
submenu = tkinter.Menu(menu)
menu.add_cascade(label=label, menu=submenu)
_set_choices(submenu, command)
elif label == '-' and command == '-':
# Separator
menu.add_separator()
else:
# Simple choice
menu.add_command(label=label, command=command)
_set_choices(menubar, choices)
The set_menu function allows us to create whole menu hierarchies ...