Tkinter - How to create submenus in menubar

Viewed 17070

Is it possible? By looking at the options I'm stumped. Searching on the web hasn't lead me anywhere. Can I create a submenu in the menubar. I'm referring to doing something similar to Idle Shell when I click on File and go down to Recent Files and it pulls up a separate file showing the recent files I've opened.

If it's not possible what do I have to use to get it to work?

2 Answers
my_menu=Menu(root) # for creating the menu bar
m1=Menu(my_menu,tearoff=0)  # tear0ff=0 will remove the tearoff option ,its default 
value is 1 means True which adds a  tearoff line
m1.add_command(label="Save",command=saveCommand)
m1.add_command(label="Save As",command=saveAsCommand)
m1.add_command(label="Print",command=printCommand)
m1.add_separator()  # this adds a separator line --this is used  keep similar options 
together
m1.add_command(label="Refresh",command=refreshCommand)
m1.add_command(label="Open",command=openCommand)

my_menu.add_cascade(label="File",menu=m1)

m2 = Menu(my_menu)
m2.add_command(label="Copy all",command=copyAllCommand)
m2.add_command(label="Clear all",command=clearAllCommand)
m2.add_command(label="Undo",command=undoCommand)
m2.add_command(label="Redo",command=redoCommand)
m2.add_command(label="Delete",command=deleteCommand)

my_menu.add_cascade(label="Edit",menu=m2)

#all the values in command attribute are functions
my_menu.add_command(label="Exit", command=quit)

root.config(menu=my_menu)

Screenshot of example

Related