How to Create Menus with tkinter Library in Python


In this example, we can learn how to create a top-level menu that contains five menu items in Python.

The Menu control is used to create three types of menus: pop-up, top-level, and pull-down. The following are the methods of the Menu control:

  • add_command(options): Add a menu item.
  • add_radiobutton(options): Create a selector button menu item.
  • add_checkbutton(options): Create a checkbox menu item.
  • add_cascade(options): Link a specified menu to its parent menu to create a new cascading menu.
  • add_separator(): Add a separator line.
  • add(type, options): Add a special type of menu item.
  • delete(startindex [, endindex]): Delete the menu item between startindex and endindex.
  • entryconfig(index, options): Modify the index menu item.
  • index(item): Returns the menu item label of the index value.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# createMenus.py

from tkinter import *
import tkinter.messagebox

# main window
win = Tk()

# handle File-New operator and display a dialog
def FileNewHandler(*arg):
    tkinter.messagebox.askokcancel("Menu", "You chose [New] menu command")

# handle File-Open operator and display a dialog
def FileOpenHandler(*arg):
    tkinter.messagebox.askokcancel ("Menu", "You chose [Open] menu command")

# handle File-Save operator and display a dialog
def FileSaveHandler(*arg):
   tkinter.messagebox.askokcancel ("Menu", "You chose [Save] menu command")

# handle Help-Document operator and display a dialog
def HelpDocumentHandler(*arg):
    tkinter.messagebox.askokcancel ("Menu", "You chose [Document] menu command")

# handle Help-About operator and display a dialog
def HelpAboutHandler(*arg):
    tkinter.messagebox.askokcancel ("Menu", "You chose [About] menu command")

# create a pull-down menu
mainmenu = Menu(win)

# new File menu and submenus
filemenu = Menu(mainmenu, tearoff=0)

filemenu.add_command(label="New", command=FileNewHandler, accelerator="Ctrl-N")
filemenu.add_command(label="Open", command=FileOpenHandler,accelerator="Ctrl-O")
filemenu.add_command(label="Save", command=FileSaveHandler,accelerator="Ctrl-S")
filemenu.add_separator()
filemenu.add_command(label="Quit", command=win.quit)
mainmenu.add_cascade(label="File", menu=filemenu)

# new Help menu and submenus
helpmenu = Menu(mainmenu, tearoff=0)
helpmenu.add_command(label="Document", command=HelpDocumentHandler,accelerator="F1")
helpmenu.add_command(label="About", command=HelpAboutHandler,accelerator="Ctrl-A")
mainmenu.add_cascade(label="Help", menu=helpmenu)

win.config(menu=mainmenu)

win.bind("", FileNewHandler)
win.bind("", FileNewHandler)
win.bind("", FileOpenHandler)
win.bind("", FileOpenHandler)
win.bind("", FileSaveHandler)
win.bind("", FileSaveHandler)
win.bind("", HelpDocumentHandler)
win.bind("", HelpAboutHandler)
win.bind("", HelpAboutHandler)

win.mainloop()

Output

The main window

After you chose the New menu

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments