Skip to content
Related Articles

Related Articles

How to create Option Menu in Tkinter ?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 31 Aug, 2021

The Tkinter package is the standard GUI (Graphical user interface) for python, which provides a powerful interface  for the Tk GUI Toolkit. In this tutorial, we are expecting that the readers are aware of the concepts of python and tkinter module.


In this tutorial, our main focus is to create option menu using tkinter in python. As the name suggests, option menu is a list of options for the user in simple language. This can be achieved using OptionMenu class, which creates a pop-up menu and displays the options that the user can avail. 

we can create an option menu by initializing the constructor of that class.

Syntax: OptionMenu(parent, variable, choice_1, choice_2, choice_3, …) 

  • parent- This is a parent object which is also called as master object. It’s main role is to create the window for GUI and also keep other widgets used in the program.
  • variable- This is the default name that should be shown to the option menu.
  • choice_1, choice_2, choice_3, …- These are the actual  list of options that are available for the user.

As OptionMenu is a class, so obviously there will be some methods in it. Let’s study some methods one-by-one.

  1. get() method- This method of OptionMenu return the value which is currently selected from the option menu.
  2. mainloop() method- This method is not only the part of OptionMenu class but also the part of whole tkinter package. It’s role is to combine all the widgets in our program before we run it and hence used at the end when we are done with our code.
  3. place() method- This method keeps all the widgets at the specified position by the programmer.
  4. pack() method- This method helps in arranging the OptionMenu in a specific position.

Let us now have a lot at some examples which will make our concepts more clear.

This is a basic example to create a simple option menu.

Example 1:


# Importing the tkinter module using import keyword
from tkinter import *
# Initialize parent object or master object as "parent"
parent = Tk()
# passing master object as parameter and set "COLOURS" as
# the name of the OptionMenu using set() method.
variable = StringVar(parent)
# Constructor of OptionMenu class initialized by giving
# the parameters as master object, variable name and the
# list of options in the menu.
option_menu = OptionMenu(parent, variable, "Yellow",
                         "Blue", "Green", "Purple",
                         "Black", "White")
# Using pack() method in OptionMenu class to arrange the
# option menu.
# Using mainloop() method from OptionMenu class before we
# run the code.


option menu tkinter

Example 2:


# Import tkinter using import keyword
import tkinter as tk
# set the master object in parent variable
parent = tk.Tk()
# Title for our window
parent.title("Geeksforgeeks- OptionMenu")
# Creating a Option Menu for AGE
# Set the variable for AGE and create the list
# of options by initializing the constructor
# of class OptionMenu.
Age_Variable = tk.StringVar(parent)
Age_Option = tk.OptionMenu(parent, Age_Variable,
                           "below 14", "15",
                           "16", "17",
                           "above 18")
# Creating a Option Menu for GENDER
# Set the variable for GENDER and create the list
# of options by initializing the constructor
# of class OptionMenu.
Gender_Variable = tk.StringVar(parent)
Gender_Option = tk.OptionMenu(parent,
                              "Male", "Female")
# Creating a Option Menu for HOBBY
# Set the variable for HOBBY and create the list
# of options by initializing the constructor
# of class OptionMenu.
Hobby_Variable = tk.StringVar(parent)
Hobby_Option = tk.OptionMenu(parent, Hobby_Variable,
                             "Dance", "Code", "Sing",
# Combining all the widgets used in the
# program before running it




tkinter option menu


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!