EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Tkinter Tutorial Tkinter OptionMenu
Secondary Sidebar
Tkinter Tutorial
  • Tkinter
    • Python Tkinter
    • Tkinter Widgets
    • Tkinter background image
    • Tkinter button color
    • Tkinter place
    • Python Tkinter Button
    • Python Tkinter Canvas
    • Tkinter Frame
    • Tkinter LabelFrame
    • Python Tkinter Label
    • Tkinter Text
    • Tkinter Scrollbar
    • Tkinter Listbox
    • Tkinter Spinbox
    • Tkinter Checkbutton
    • Tkinter Menu
    • Tkinter Menubutton
    • Tkinter OptionMenu
    • Tkinter Messagebox
    • Tkinter Grid
    • Tkinter Pack
    • Tkinter Scale
    • Tkinter Table
    • Python Tkinter Entry
    • Tkinter after
    • Tkinter Colors
    • Tkinter Font
    • Tkinter PhotoImage
    • Tkinter TreeView
    • Tkinter Notebook
    • Tkinter Combobox
    • Tkinter Bind
    • Tkinter Icon
    • Tkinter Window Size
    • Tkinter Color Chart
    • Tkinter Slider
    • Tkinter Calculator
    • Tkinter geometry
    • Tkinter image
    • Tkinter input box
    • Tkinter mainloop

Tkinter OptionMenu

By Shalu SharmaShalu Sharma

Tkinter OptionMenu

Definition of Tkinter OptionMenu

As the name suggests here OptionMenu represents some options available to the user. We can implement this OptionMenu in our application where we have some fixed type of options which need to be display and out of which we can select one. This is similar to the drop-down functionality available in many languages. Here Tkinter is used to create GUI in python and it is most commonly used library we have. By the use of it we can create an GUI very easily and very fast.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

To make use of OptionMenu we first need to import the Tkinter package into our program and we can do this by following some steps which are mentioned below.

1) First: First we will import the Tkinter package into our program because we need to pass its object while calling OPtionMenu construct so it is needed, for creating the main window.

e.g. :

from Tkinter import *

2) Calling constructor: Here we are preparing our Option menu which will represent some options to the user.

e.g. :

OptionMenu(parent, var, ch1, ch2, ch3, ...)

This is the basic syntax to create options menu in python using Tkinter. We will see some practice examples in the coming section in more detail.

How OptionMenu Works in Tkinter?

As now we know that OptionMenu represents options values. This is very important when it comes to select from the available values, also it represents only one value at a time. This OptionMenu is called a drop-down list, select option, and many more in other languages. They are also useful when we want to perform some specific operation one select of value from OptionMenu or we may require to hide or show some fields or values to the user. We can also trigger some events when we select values from the OptionMenu. This is the part of Tkinter, so in order to use this widget into our GUI, we need to have Tkinter module present in our application or program. This OptionMenu makes our application very much interactive with the user and also they are very easy and fast to implement by using the OptionMenu.

Now we will someone practice example to understand how to create OptionMenu in python for this below is one example for beginners to start with;

e.g. :

from Tkinter import *
parent = Tk()
varList = StringVar(parent)
varList.set("Red")
om = OptionMenu(parent, varList, "Yellow", "Pink", "RED", "Brown", "Black", "Blue")
om.pack()

1) In the above example we are creating one OptionMenu here which is contains some values inside it. To create the OptionMenu first thing we have done is to import the Tkinter module into our program. After this we are creating an object for Tkinter, this is important because it will go to create a window for us inside which we can have our menus and other stuff available. So this will be the master or the parent window here.

2) After this we are setting an initial value for our OptionMenu, it will be that value when none of the value will be selected by the user and this is going to show in the OptionMenu as the default value we can say. But in order to do so this value should be present in the list as well when we will pass them into the constructor.

3) After this all we are calling the OptionMenu constructor which takes a different parameter as the input here. So this step will complete our creation for OptionMenu. We will pass all the parameters inside this constructor i.e. parent our Tkinter object, varList our options list, choices for the user to select for them.

It also provides us some methods to get the selected value from the OptionMenu we will discuss in a later section in detail.

Constructor

As we have already seen its constructor that it takes a number of parameters as the input. Which represent different input that it takes. This will contain the object, list, and choices for optionMenu. Now we will discuss each parameter in detail what they take and how they behave see below;

This is the constructor definition which takes three parameters in main, which are as follows;

OptionMenu(parent, var, ch1, ch2, ch3, ...)

1) parent: This will be the Tkinter object which will be responsible to create the window for the GUI where we will create and how our menu, OptionMenu, and many more widgets. So it takes the parent object or master object.

2) var: This is the variable that is used to show the default value to the OptionMenu. We can set the default value here from the OptionMenu. So this will show to the user as the default value without select any value.

3) choice1: This is the list of the variable or we can say a list of the OptionMenu. We can specify any number of choices here which will be showing in the OptionMenu as the list of variables.

Methods

Option Menu we have some methods which is used to get the name of the OptionMenu and create the window for the GUI. Let’s discuss them in detail which is mentioned below as follows;

1) get(): This method of OptionMenu is used to get the value from the OptionMenu which is selected at current. It will give us the value from the OptionMenu.

2) mainLoop(): This method is used when our application on GUI is ready to run. We can call it at the end when it is ready to run.

3) place (): This method is used to place all the widgets into the specified position into the window.

4) pack(): This method will arrange the OptionMenu into the specified position after that it will arrange them into the master or parent window.

Example of Tkinter OptionMenu

In this example, we are creating optinMenu.

from Tkinter import *
parent = Tk()
varList = StringVar(parent)
varList.set("Red")
om = OptionMenu(parent, varList, "Yellow", "Pink", "RED", "Brown", "Black", "Blue")
om.pack()

Output:

tkinter OptionMenu

 Conclusion

This OptionMenu is used to provide options to the user. We can give any number of variables as the list. This is the part of the Tkinter module. we can use different widgets to make it interactive to the user also we can perform many events to trigger delete, save, and many more operations.

Recommended Articles

This is a guide to Tkinter OptionMenu. Here we discuss the definition and working of Tkinter OptionMenu along with examples and code implementation. You may also have a look at the following articles to learn more –

  1. Tkinter Menubutton
  2. Tkinter Scrollbar
  3. Tkinter Widgets
  4. Tkinter Grid
Popular Course in this category
Tkinter Course (1 Course, 1 Project)
  1 Online Courses |  1 Hands-on Projects |  6+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more