pythontkinterpython-3.8tkinter-layouttkinter-menu

How do you make a drop down menu in Tkinter?


I have a quick question, how would you make a drop down menu in Tkinter like the one below:

enter image description here

This menu has a drop down option, how would you add a drop down in tkinter here is my code:

# Menu Bar 
MenuBar = Menu(root)
root.config(menu=MenuBar)
MenuBar.config(bg="White", fg="Black", activebackground="Whitesmoke", activeforeground="Black", activeborderwidth=1, font=('Monaco', 11))

# Settings Option
SettingsOption = Menu(MenuBar, tearoff=False)
MenuBar.add_cascade(label="Settings", menu=SettingsOption)
SettingsOption.add_command(label="Help", command=None)
SettingsOption.add_command(label="Documentation", command=None)

So whenever I click Settings I should get a menu called help. Then when I hover over help I should get another dropdown menu called documentation. How would you do this in Python Tkinter?


Solution

  • You can use add_cascade() to add sub-menu:

    import tkinter as tk
    
    root = tk.Tk()
    
    menubar = tk.Menu(root)
    menubar.config(bg="white", fg="black", activebackground="whitesmoke", activeforeground="black", activeborderwidth=1, font="Monaco 11")
    
    settings_menu = tk.Menu(menubar, tearoff=False)
    
    help_menu = tk.Menu(settings_menu, tearoff=False)
    help_menu.add_command(label="Documentation")
    
    settings_menu.add_cascade(label="Help", menu=help_menu)
    menubar.add_cascade(label="Settings", menu=settings_menu)
    
    root.config(menu=menubar)
    root.mainloop()
    

    enter image description here