Tkinter Menu Create submenu
The concepts used when creating the menu are as follows.
menubar = Menu(root)
filemenu = Menu(menubar)
menu.add_cascade(label="File", menu=filemenu)
The above is to create a File menu. The so-called build submenu is to create a separate submenu within the File menu. If the submenu to be created is Find submenu, the object to be created is findmenu, then you can use the following command.
findmenu = Menu(filemenu)
xxx # This is to create a list of submenus
xxx # This is to create a list of submenus
filemenu.add_cascade(label="Find", menu=findmenu)
Example#1
Create the Find submenu within the File menu, this submenu has Find Next and Find Pre commands.
from tkinter import *
from tkinter import messagebox
def findNext():
messagebox.showinfo("Find Next","Find Next")
def findPrevious():
messagebox.showinfo("Find Previous","Find Previous")
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
menubar = Menu(root) # Create top-level menu
# Create a menu category object, and name this menu category as File
filemenu = Menu(menubar)
menubar.add_cascade(label="File",menu=filemenu,underline=0)
# Create a menu list within the File menu
# First create the find submenu object within the File menu
findmenu = Menu(filemenu,tearoff=False) # Cancel separator False
# filemenu.add_cascade(label="Find",menu=findmenu) ############## You can go here earlier
findmenu.add_command(label="Find Next",command=findNext)
findmenu.add_separator() ########################################
findmenu.add_command(label="Find Previous",command=findPrevious)
filemenu.add_cascade(label="Find",menu=findmenu) ##############
# Here's how to add a separator line and create an Exit! submenu
filemenu.add_separator()
filemenu.add_separator()############################################
filemenu.add_command(label="Exit!",command=root.destroy,underline=0)
root.config(menu=menubar)
root.mainloop()
Output:
As the design of the submenu in general for the omission of dashed separator design, so I added tearoff = False
in the Menu () method in line 19.