Tkinter Combobox

Tkinter Combobox is the drop-down list for the user to choose from. It is a combination of Entry and drop-down menu as you can see. When you click the arrow on the left side, you will see a drop-down menu showing all the choices, and if you click on one, it will replace the current Entry contents.

Python Tkinter Combobox Example

Tkinter Combobox_Basic.py
import tkinter as tk
from tkinter import ttk
from sqlalchemy.sql.expression import column
 
app = tk.Tk() 
app.geometry('200x100')

labelTop = tk.Label(app,
                    text = "Choose your favourite month")
labelTop.grid(column=0, row=0)

comboExample = ttk.Combobox(app, 
                            values=[
                                    "January", 
                                    "February",
                                    "March",
                                    "April"])
pprint(dict(comboExample)) 
comboExample.grid(column=0, row=1)
comboExample.current(1)

print(comboExample.current(), comboExample.get())

app.mainloop()

Tkinter Tutorial Combobox Basic

from tkinter import ttk

Tkinter combobox widget is in the ttk module of Tkinter, therefore, you need to import this module to use this widget.

comboExample = ttk.Combobox(app, 
                            values=[
                                    "January", 
                                    "February",
                                    "March",
                                    "April"])

It creates the Tkinter combobox widget instance and also assigns the values that will appear in the drop-down list.

comboExample.current(1)

Normally, the default item showing in the combobox is the first element in the values list. You could also change it to any element by using current(index) method.

print(comboExample.current(), comboExample.get())

You could also use current() to get the index of the current selected element, and use get() method to get the element itself.

Tkinter Combobox Event Binding

The combobox callback function binding is different from the widgets introduced in last chapters. bind() method is the way to bind the callback function with the combobox virtual event when the user selects an element in the drop-down list.

Tkinter Combobox_Virtual Event Binding.py
import tkinter as tk
from tkinter import ttk
from sqlalchemy.sql.expression import column

def callbackFunc(event):
     print("New Element Selected")
     
app = tk.Tk() 
app.geometry('200x100')

labelTop = tk.Label(app,
                    text = "Choose your favourite month")
labelTop.grid(column=0, row=0)

comboExample = ttk.Combobox(app, 
                            values=[
                                    "January", 
                                    "February",
                                    "March",
                                    "April"])


comboExample.grid(column=0, row=1)
comboExample.current(1)

comboExample.bind("<<ComboboxSelected>>", callbackFunc)


app.mainloop()

def callbackFunc(event):
     print("New Element Selected")

This is the defined callback function whenever you select an element from the list.

Note
event shall not be skipped as the passing argument, it is passed from the combobox virtual event
comboExample.bind("<<ComboboxSelected>>", callbackFunc)

It binds the virtual event <<ComboboxSelected>> with the callback function.

Every time you select a new element from the list, it prints New Element Selected