faqts : Computers : Programming : Languages : Python : Tkinter

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 8 people (88%) answered Yes
Recently 7 of 8 people (88%) answered Yes


How do I add nested sub-menu's in a drop down menu?

Jul 13th, 2003 04:33
Daniel Haertle, David Cummings,

You add nested submenus with the the add_cascade(menu=yoursubmenu) 
method of the Menu widget. 
# File: slightly modified "menu1.py" from "An introduction to TkInter"
from Tkinter import *
def callback():
    print "called the callback!"
root = Tk()
# create a toplevel menu
menu = Menu(root)
# create File menu
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=callback)
filemenu.add_command(label="Open...", command=callback)
filemenu.add_command(label="Exit", command=callback)
# create Cascaded menu
cascadedmenu = Menu(menu)
# first create the last level
cascadedjazzmenu = Menu(cascadedmenu)
cascadedjazzmenu.add_command(label="good Jazz", command=callback)
cascadedjazzmenu.add_command(label="excellent Jazz", command=callback)
# then secondlast level
cascadedmenu.add_cascade(label="Jazz", menu=cascadedjazzmenu)
# then first level
menu.add_cascade(label="Cascaded", menu=cascadedmenu)