Tkinter Combobox

Summary: in this tutorial, you’ll learn how to create Tkinter combobox widgets.

Introduction to the Tkinter Combobox

A combobox is like a combination of an Entry widget and a Listbox widget. A combobox allows you to select one value in a list of values. In addition, it allows you to enter a custom value.

To create a combobox, you’ll use the ttk.Combobox() constructor:

widget_var = tk.StringVar() combobox = ttk.combobox(container, textvariable=widget_var)
Code language: Python (python)

The container is the window or frame on which the Combobox widget locates

The textvariable argument links a variable to the current value of the combobox. The variable needs to be an instance of the tk.StringVar().

To get current selected value, you can use the get() method:

current_value = combobox.get()
Code language: Python (python)

And to set the current value, you can use the set() method:

Code language: Python (python)

Once the combobox is created, you can supply a list of predefined values using the values option:

compbobox['values'] = ('value1', 'value2', 'value3')
Code language: Python (python)

By default, you can enter a new entry in the combobox. If you don’t want this, you can set the state option to 'readonly':

compbobox['state'] = 'readonly'
Code language: Python (python)

To re-enable editing the combobox, you use the 'normal' state like this:

compbobox['state'] = 'normal'
Code language: Python (python)

A combobox will generate a '<<ComboboxSelected>>' virtual event that you can bind to whenever its selected value changes:

combobox.bind('<<ComboboxSelected>>', fn)
Code language: JavaScript (javascript)

Python Tkinter combobox example

The following program illustrates how to create a combobox widget:

import tkinter as tk from tkinter import ttk from tkinter.messagebox import showinfo root = tk.Tk() root.geometry('300x200') root.resizable(False, False) root.title('Combobox Widget') def month_changed(event): msg = f'You selected {month_cb.get()}!' showinfo(title='Result', message=msg) # month of year months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec') label = ttk.Label(text="Please select a month:") label.pack(fill='x', padx=5, pady=5) # create a combobox selected_month = tk.StringVar() month_cb = ttk.Combobox(root, textvariable=selected_month) month_cb['values'] = months month_cb['state'] = 'readonly' # normal month_cb.pack(fill='x', padx=5, pady=5) month_cb.bind('<<ComboboxSelected>>', month_changed) root.mainloop()
Code language: Python (python)



  • Use ttk.Combobox(root, textvariable) to create a Combobox.
  • Set the state property to readonly to prevent users from entering custom values.
  • Bind a function to the '<<ComboboxSelected>>' event so that it’s invoked automatically when the selected value changes.
Did you find this tutorial helpful ?