×

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 Linked List

Linked List Linked lists non-contiguous linear data structure which is made up of nodes and used to store value and a pointer pointing to the next node. It is linked with...

6 minutes read.

Python String title() method

Python String title() method The string.title() method in Python returns a string where the first character in every word is upper case. If the word contains a number or a symbol,...

1 minute read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Python List sort() method

The list.sort () method in Python sorts the items of the list in place. Syntax list.sort(key=None, reverse=False) Parameter reverse: If a Boolean value ‘True’ is passed, the  sorting will be done in the descending order else for ‘False’...

2 minutes read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.

Python Operator Precedence

Before knowing about the operator precedence in Python, we have to know about the operators in Python. So let's have a look at it. According to one definition, the operator is...

3 minutes read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

Python SciPy Library

Introduction SciPy, a logical library for Python is an open-source, BSD-authorized library for arithmetic, science, and design. The SciPy library relies upon NumPy, which gives advantageous and quick N-dimensional exhibit control....

6 minutes read.

Python Not Equal Operator

Python provides us with many operators to make tasks easier. There are about 7 categories of operators in Python. One of the 7 classifications is the comparison operators. Just as...

3 minutes read.

Programs for Printing Pyramid Patterns in Python

<!-- wp:paragraph --><p>Python supports printing patterns using basic for loops. The number of rows is handled by the first outer loop, while the number of columns is handled by the...

9 minutes read.

Python String replace() method

Python String replace() method The string.replace() method in Python returns a copy of the string with all occurrences of substring old replaced by new. Syntax replace(old, new[, count]) Parameter old: This parameter represents a substring you want to...

2 minutes read.

Python Knapsack problem

Python Knapsack problem Before we dig down about Knapsack problems in Python, first let's have a look at what is actually a knapsack problem. What is a knapsack problem? A problem from the...

5 minutes read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Front end in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

4 minutes read.

Anaconda python 3.7 download for windows 10 64-bit

Before installing Anaconda, you need to first know what Anaconda actually is. Anaconda is an IDE (Integrated development environment) platform used to code and executes python programs. Anaconda works both online...

3 minutes read.

Python Printf Style Formating

String objects have one special implicit activity: the % administrator (modulo). This is otherwise called the string designing or interjection administrator. Given design % values (where configuration is a string),...

3 minutes read.

Python String endswith() method

Python String endswith() method The string.endswith() method in Python returns a Boolean value True if the string ends with the specified suffix, otherwise it returns False. Syntax endswith(suffix[, start[, end]]) Parameter suffix – This parameter represents a string or tuple...

2 minutes read.

Python Set isdisjoint() method

Python Set isdisjoint() method The set.isdisjoint() method in Python returns a boolean value True if two sets are disjoint sets ( i.e. none of the elements are present in both sets), otherwise it returns...

1 minute read.