Here we are providing basic tutorial in Tkinter on how to create a dropdown menu in a Tkinter GUI application.

Sample code

The following Tkinter code will demonstrate the same:

from tkinter import *

def doNothing():
    print("Ok Ok I wont")

root = Tk()

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

subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project...", command=doNothing)
subMenu.add_command(label="New...", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing)

editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=doNothing)

root.mainloop()


In the above code, we had defined a method called doNothing() which prints something. In the next few lines of code, we created a menu called menu using the pre-defined method in Tkinter known as Menu which takes root window as a parameter. Then we created two sub-menus called as subMenu and editMenu. add_cascade() is used to add sub-menus by mentioning their names and labels. add_command is used to assign a particular command or a method to a particular option in a menu for example in above code, an option New Project... is assigned a method doNothing which actually do nothing except printing some text. add_separator() is used to seperate options in a menu by having a geometrical line between them. In the GUI application, we had created two sub-menus called as File and Edit which are further having some options as shown below.

Output

output