×

ComboBox in Python

Python includes several graphical user interface (GUI) libraries, including PyQT, Tkinter, Kivy, WxPython, and PySide. Tkinter is the most commonly used GUI module in Python because it is simple and easy to understand.

Tkinter is a fork of the Tk interface. The tkinter module is included in the Python standard library and must be imported when writing a Python programme

that generates a graphical user interface.

A Combobox is a cross between a list box and a text entry field. It's a Tkinter widget with a down arrow for choosing from a list of options.

It allows users to select from a list of displayed options. When the user clicks on the drop-down arrow in the entry field, a pop-up of the scrolled Listbox appears down the entry field. Only after selecting an option from the Listbox will the selected option be displayed in the entry field.

The combobox widget's syntax is as follows:

combobox = ttk.Combobox(master, option=value, ...)

Example 1: Combobox widget without setting a default value.

We can create a combobox widget without setting a default value. In this example we will look at the approach for the same.

import tkinter as tk
from tkinter import ttk


window = tk.Tk()
window.title('Combobox')
window.geometry('500x250')
ttk.Label(window, text = "JTP Combobox Widget",
		background = 'black', foreground ="white",
		font = ("Times New Roman", 15)).grid(row = 0, column = 1)


# label
ttk.Label(window, text = "Select the day :",
		font = ("Times New Roman", 10)).grid(column = 0,
		row = 5, padx = 10, pady = 25)


# Combobox creation
n = tk.StringVar()
daychoosen = ttk.Combobox(window, width = 27, textvariable = n)


# Adding combobox drop down list
daychoosen['values'] = (' Monday','Tuesday','Wedenesday','Thursday','Friday','Saturday','Sunday')


daychoosen.grid(column = 1, row = 5)
daychoosen.current()
window.mainloop()

Output for the above program is as follows:

ComboBox in Python
ComboBox in Python

Example 2: Combobox with initial default values.

We can also set the initial default values in the Combobox widget as shown in the below sample code.

import tkinter as tk
from tkinter import ttk
window = tk.Tk()
window.title('Combobox')
window.geometry('500x250')


ttk.Label(window, text = "JTP Combobox Widget",
		background = 'black', foreground ="white",
		font = ("Times New Roman", 15)).grid(row = 0, column = 1)
# label
ttk.Label(window, text = "Select the day :",
		font = ("Times New Roman", 10)).grid(column = 0,
		row = 5, padx = 10, pady = 25)
# Combobox creation
n = tk.StringVar()
daychoosen = ttk.Combobox(window, width = 27, textvariable = n)
# Adding combobox drop down list
daychoosen['values'] = (' Monday','Tuesday','Wedenesday','Thursday','Friday','Saturday','Sunday')
daychoosen.grid(column = 1, row = 5)
daychoosen.current(0)
window.mainloop()
ComboBox in Python

Related Topics

Python delattr() function

Python delattr() function The delattr() function in Python is used to delete the named attribute from the object, with the prior permission of the object. Syntax delattr(object, name) Parameter object : This parameter represents the object from which...

1 minute read.

Data Class in Python

Introduction In Python 3.7, the dataclass modules are introduced as a practical tool for creating organized classes designed specifically for data storage. These classes contain specific attributes and capabilities to deal...

6 minutes read.

How to run Python program in CMD

How to run python program in cmd Command Prompt provides us all together with a different approach for dealing with our programs. The programs can be executed by accessing the directories. In...

3 minutes read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Spark and Python for big data with pyspark github

Pyspark: Apache Spark is a computational engine that handles large data sets using parallel and batch processing. Apache Spark is an open-source, distributed computing framework and collection of libraries for real-time,...

3 minutes read.

How to build an Auto Clicker using Python?

What is an Auto Clicker? An Auto Clicker is software that is used for automating the click of a mouse on any particular element on the computer screen and controlling the...

8 minutes read.

Python Dictionary update() method

Python Dictionary update() method The dictionary.update() method in Python inserts the specified items to the dictionary. Syntax dictionary.update(iterable) Parameter iterable- This parameter represents a dictionary or an iterable object with key value pairs, that will...

1 minute read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

Python PPTX

Python-pptx is a python library that enables the user to create and update PowerPoint (.pptx) presentations. It is used to create modified PowerPoint presentations from the db content that can...

10 minutes read.

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

What is online python free IDE

An IDE is an acronym that stands for “Integrated development environment.” It is a software environment that has a software development package that consists of the code editor and builds...

4 minutes read.

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

Python whois

What is whois? Whois is a protocol used to identify the owner of the registered domain name. It is a querying database that is used to record the registered users. WHOIS is...

4 minutes read.

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python Interpreter

In this tutorial, we will go through the basic knowledge about what an interpreter is and how we use it in the python programming language. It is one of the...

3 minutes read.

Python lambda() Function

In this tutorial, we will learn about a new concept known as the Lambda function. It is a relatively new concept while learning Python. Python lambda functions are anonymous functions. It...

3 minutes read.

Add Dictionary to Dictionary in Python

Dictionary in python: Python's execution of a data model, known more commonly as an implicit array, is a dictionary. A dictionary is made up of a group of key-value pairs. Each...

4 minutes read.