menu in python code example

Example 1: how to make menu in tkinter

from tkinter import *

def do_some():
    print('Do some!')

root = Tk()

main_menu = Menu(root)
root.config(menu=main_menu)


# Type 1 Menu
main_menu.add_command(label='File', command=do_some)
# Type 1 Menu


# Type 2 Menu
file_menu = Menu(main_menu)
main_menu.add_cascade(label='File', menu=file_menu)

file_menu.add_command(label='Save', command=do_some)
file_menu.add_command(label='Save As', command=do_some)
# Type 2 Menu


# Type 3 Menu
file_menu = Menu(main_menu)
main_menu.add_cascade(label='File', menu=file_menu)

save_menu = Menu(file_menu)
save_as_menu = Menu(file_menu)

file_menu.add_cascade(label='Save', menu=save_menu)
file_menu.add_cascade(label='Save As', menu=save_as_menu)

save_menu.add_command(label='New', command=do_some)
save_menu.add_command(label='Old', command=do_some)
save_as_menu.add_command(label='New', command=do_some)
save_as_menu.add_command(label='Old', command=do_some)
# Type 3 Menu

root.mainloop()

Example 2: python menu

from tkinter import *
from tkinter import messagebox # just for example not necessary

# some example fungtions
def Res():
    root.geometry('333x222')
def B2N():
    root.geometry('444x444')
def Full():
    root.geometry('1000x700')
def custom_quit():
    answer=tkinter.messagebox.askokcancel("Are you Sure?","Your Data will be lost if you exit")
    if (answer):
        quit()
        
#the main window
root=Tk()
root.geometry('444x444')
text=Label(root,text='*my first menu*').pack()
# the actual menu (used a big example for better understanding of menu and sub-menu)
menu1=Menu(root)
root.configure(menu=menu1)

submenu1=Menu(menu1)
menu1.add_cascade(label='File',menu=submenu1)
sub2menu=Menu(submenu1)
sub2menu.add_command(label='Recent')
sub2menu.add_command(label='Browse')

submenu1.add_command(label='New File')
submenu1.add_cascade(label='Open File',menu=sub2menu)
submenu1.add_command(label='Save As')
submenu1.add_command(label='Print')
submenu1.add_command(label='Quit', command=custom_quit) # adding command to menu elements

submenu2=Menu(menu1)
menu1.add_cascade(label='Edit',menu=submenu2)
submenu2.add_command(label='Undo')
submenu2.add_cascade(label='Redo')
submenu2.add_command(label='Cut')
submenu2.add_command(label='Copy')
submenu2.add_command(label='Paste')
submenu2.add_command(label='Select All')

submenu3=Menu(menu1)
menu1.add_cascade(label='Format',menu=submenu3)
submenu3.add_command(label='Indent Region')
submenu3.add_cascade(label='Comment')
submenu3.add_command(label='All')
submenu3.add_command(label='Selected')
submenu3.add_command(label='Format Paragraph')

submenu4=Menu(menu1)
menu1.add_cascade(label='Run',menu=submenu4)
submenu4.add_command(label='Python Shell')
submenu4.add_cascade(label='Run Module')
submenu4.add_command(label='Check Module')

submenu5=Menu(menu1)
menu1.add_cascade(label='Options',menu=submenu5)
submenu5.add_command(label='Configure IDLE')
submenu5.add_cascade(label='Show Code Context')
submenu5.add_command(label='Zoom Height')

submenu6=Menu(menu1)
menu1.add_cascade(label='Window',menu=submenu6)
submenu6.add_command(label='Python 3.7.3 shell')
submenu6.add_cascade(label='Untitled')
submenu6.add_command(label='Resize window',command=Res)
submenu6.add_command(label='Norml size window',command=B2N)
submenu6.add_command(label='Large window',command=Full)

submenu7=Menu(menu1)
menu1.add_cascade(label='Help',menu=submenu7)
submenu7.add_command(label='About IDLE')
submenu7.add_cascade(label='IDLE Help')
submenu7.add_command(label='FAQ')
submenu7.add_command(label='Contact US')