Python Tkinter Combobox Event Binding
Combobox is an important widget, it is found in many applications. You have generally seen this on a Macintosh and Windows interface. It allows user to select from a lists of options. In this article, you will learn how to call function on select option from combobox using Tkinter.
Import the Module
The combobox widget is a class of ttk module of Tkinter library. To start using Ttk, first import the Ttk module follow the Tk module. By this way, we override the widgets of Tk module with Ttk module. The ttk module provides better look and feel among many platforms. It is more modern and configured with styles.
In the previous article, we have 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 combobox event when the user select an option from the dropdown lists. The syntax of bind() method is -
It any event matching with the given event occurs in the widget, the given function is called with an object describing the event. The combobox widgets generates a <<ComboboxSelected>> virtual event when the user selects an element from the list of values.
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()
Related ArticlesPython Tkinter Geometry Managers
Python Tkinter Scale Widget
Python Tkinter Overview and Examples
Python3 Tkinter Messagebox
Entry Field Validation in Tkinter Python
Python 3 Tkinter Menu Bar
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml