- Python Tkinter Combobox Example
- Tkinter Combobox Event Binding
- Tkinter Combobox Dynamically Update Values
- Tkinter Combobox Read Only
Tkinter Combobox is the drop-down list for the user to choose from. It is a combination of
drop-down widgets 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
Python Tkinter Combobox Example
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.
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
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.
def callbackFunc(event): print("New Element Selected")
This is the defined callback function whenever you select an element from the list.
event shall not be skipped as the passing argument, it is passed from the combobox virtual event
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.
Tkinter Combobox Dynamically Update Values
The list of choices in Tkinter combobox could be dynamically updated with the option
postcommand that executes the given function before displaying the pop-down list of choices.
import tkinter as tk from tkinter import ttk def callbackFunc(event): print("New Element Selected") app = tk.Tk() app.geometry('200x100') def changeMonth(): comboExample["values"] = ["July", "August", "September", "October" ] labelTop = tk.Label(app, text = "Choose your favourite month") labelTop.grid(column=0, row=0) comboExample = ttk.Combobox(app, values=[ "January", "February", "March", "April"], postcommand=changeMonth) comboExample.grid(column=0, row=1) app.mainloop()
changeMonth function changes the combobox list of choices to be
["July", "August", "September", "October" ]
The combobox shows empty after GUI starts and shows the given months as above after user clicks the arrow in the combobox.
Tkinter Combobox Read Only
The content of the elements in the Combobox examples shown above is editable because the default
normal that makes the text field directly editable.
Tkinter Combobox has the other two states besides
readonly- the text field is not editable, and the user could only select the values from the drop-down list.
disabled- the Combobox is grayed out and interaction is impossible.
import tkinter as tk from tkinter import ttk 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"], state="readonly") comboExample.grid(column=0, row=1) comboExample.current(0) print(comboExample.current(), comboExample.get()) app.mainloop()
The above example sets the Tkinter Combobox read-only.
If you change the state from
disabled, the Combobox is grayed out as shown below.