简体   繁体   中英

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:

在此处输入图像描述

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?

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM