Python Tkinter Combobox Event Binding
Combobox is an important widget, and it is found in many applications. It allows a user to select from a list of options. It holds multiple values but shows only one option at a time. You have generally seen this on a Macintosh or Windows interface. In this article, you will learn how to call a function on selecting an option from a combobox using Python Tkinter.
Import the Module
The combobox widget is a class of the ttk module of the Tkinter library. To start using Ttk, first import the Ttk module and follow the Tk module. By this way, we override the widgets of the Tk module with the Ttk module. The ttk module provides a better look and feel across many platforms. It is more modern and configured with styles.
Tkinter combobox bind()
In the previous article, we introduced a simple example of Tkinter combobox. Here you will learn the Tkinter combobox event binding process. The bind() method is used to bind the callback function with the combobox event when the user selects an option from the dropdown lists. The syntax of bind() method is-
widget.bind("event", function)
If any event matching the given event occurs in the widget, the given function is called with an object describing the event. The combobox widgets generate a <<ComboboxSelected>> virtual event when the user selects an element from the list of values.
Example of Tkinter Combobox Event Binding
The following Python program demonstrates how to bind the callbackFunc() function to the ComboboxSelected key pressed event of the 'Select the Country' button. The callbackFunc() function changes the combobox list of choices.
import tkinter as tk
from tkinter import ttk
# Creating tkinter window and set dimensions
window = tk.Tk()
window.title('Combobox')
window.geometry('500x250')
def callbackFunc(event):
country = event.widget.get()
print(country)
# label text for title
ttk.Label(window, text = "Choose the country and vote for them",
background = 'cyan', foreground ="black",
font = ("Times New Roman", 15)).grid(row = 0, column = 1)
# Set label
ttk.Label(window, text = "Select the Country :",
font = ("Times New Roman", 12)).grid(column = 0,
row = 5, padx = 5, pady = 25)
# Create Combobox
n = tk.StringVar()
country = ttk.Combobox(window, width = 27, textvariable = n)
# Adding combobox drop down list
country['values'] = (' India',
' China',
' Australia',
' Nigeria',
' Malaysia',
' Italy',
' Turkey',
' Canada')
country.grid(column = 1, row = 5)
country.current()
country.bind("<<ComboboxSelected>>", callbackFunc)
window.mainloop()
Output of the above code
Related Articles
Python Tkinter Tutorial with ExamplesPython Tkinter Combobox
Python Tkinter Geometry Managers
Python3 Tkinter Messagebox
Python Tkinter Frame Widget
Python Tkinter Scale Widget
Entry Field Validation in Tkinter Python
Python Tkinter Text Widget
Python Tkinter Checkbutton Widget
Python 3 Tkinter Menu Bar
Add background image in Python Tkinter
OpenCV histogram equalization color
Color histogram Python OpenCV
Histogram of grayscale image python
High pass filter OpenCV python
Python OpenCV ColorMap
OpenCV Gaussian blur Python
Python OpenCV Overview and Examples
OpenCV Logical Operators- Bitwise AND, OR, NOR, XOR
Arithmetic Operations on Images using Python OpenCV
Adaptive Thresholding in Python OpenCV
Geometric Transformation OpenCV Python
Python OpenCV Erosion and Dilation
Color detection OpenCV Python