Creating a Dropdown Menu using Tkinter

PythonTkinterGUI-Programming

Navigation is the most important part of any application, as it improves the user experience in an aesthetic way. Using Tkinter, we can create menus and submenus very efficiently.

Tkinter has an inbuilt function to create menus and these can be invoked with another tkinter widget or window. Tkinter.Menu module provides some properties in Menu-items. Some of these properties are used to label the buttons, toggling the button, adding submenus using cascade properties, etc.

In this article, we will see how to create a dropdown menu using tkinter.Menu and its Menu-item properties. We will use OptionMenu widget to create a list of options and related commands.

Example

from tkinter import *
win =Tk()
win.geometry("700x300")
label= Label(win, text= "Select any One Language!", font= ("", 10))
label.pack(pady=30)

#Access the Menu Widget using StringVar function
clicked= StringVar()
#Create an instance of Menu in the frame
main_menu = OptionMenu(win, clicked, "C++", "Java", "Python", "Rust","Go","Ruby")
main_menu.pack()

win.mainloop()

Output

Running the above code will create a dropdown Menu.

raja
Published on 04-Mar-2021 13:35:44
Advertisements