Combo Boxes in Tkinter (GUI Programming) – Python Tutorial

Combobox: Combobox widget in Tkinter is a combination of Listbox and an entry field. This widget contains dropdown arrow. We can select from a list of many options. This widget is also very widely used in forms. It helps the users to select choice according to the list of options displayed in Combobox. A pop up of scrolled Listbox is displayed when user clicks on the drop-down arrow. Combobox also follows the default value setting option. If we do not set any value to default, then it will display nothing selected by-default.

python-video-tutorials

Syntax:

cbox = tk.Combobox(root, value = options)

Let’s see one example to understand concept of combobox better

Example 1: Without setting any default value

from tkinter import *
from tkinter import ttk as tk

#root window
root = Tk()

#Combobox
fruits = StringVar()
entries = ("Mango", "Apple", "Banana", "Guava",)
box = tk.Combobox(root, value= entries, textvariable = fruits).pack(pady=20)


root.geometry("250x200")
root.title("PythonLobby")
mainloop()

Output:

combobox-in-tkinter-python

Example 2: Combobox with setting default value

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

var = tk.StringVar()
fruit = ttk.Combobox(root, width = 27, textvariable = var)

# Adding combobox drop down list
fruit['values'] = (' Guava', ' Mango', ' Apple', ' Banana',)

fruit.pack(pady=20)

# Setting Apple as a default
fruit.current(2)

root.geometry('350x250')
root.title("PythonLobby")
root.mainloop()

Output:

combobox-in-python-tkinter

gui-in-python

Program Explanation in Detail

    1. We have imported all modules of tkinter and ttk.
    2. Next we have initialized root as object variable for Tk() class. Which is responsible for creating root window.
    3. Combobox is the widget which is available in ttk module of tkinter, that’s why we have imported
      from tkinter import ttk as tk
      
    4. So we will be using tk as reference for creating Combobox.
    5. There are three parameters, which are necessary to pass while creating Combobox i.e. root, value and textvariable
    6. Here parameter root used for root window, value used for the values that we want to show in our Combobox as available options and textvariable is the parameter is used to check the type of options. It may be Integer or String.
    7. For integer type we use IntVar() and for string we use StringVar() functions. Here we are using StringVar() because we are using string type options.

Important: 

combo-box-in-python-tkinter

Here in this Combobox example, we can observe that our available options can be update. And this is not the correct way to create Combobox. To solve this type of problem ttk module provides “state” parameter. If we set state = “readonly” than the option cannot be update.

Example 3: All code is same as above but here we have only introduced the use of state parameter.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

var = tk.IntVar()
fruit = ttk.Combobox(root, width = 27, textvariable = var, state="readonly")

# Adding combobox drop down list
fruit['values'] = (' Guava', ' Mango', ' Apple', ' Banana',)

fruit.pack(pady=20)

# Setting Apple as a default
fruit.current(2)

root.geometry('350x250')
root.title("PythonLobby")
root.mainloop()

Output:

combo-box-in-tkinter