Combobox Widget in Python Tkinter
Python is a popular programming language that is used to build various applications. One of the significant aspects of Python is its built-in GUI toolkit, Tkinter, which helps developers to create a graphical user interface for their applications. Tkinter has multiple widgets that programmers can use to create user-friendly applications quickly. One of such widgets is the Combobox.
In this article, we will learn about the Combobox widget, how to create a Combobox widget using Python Tkinter, and add data to it dynamically. Combobox widget is a dropdown list of options that users can select from. It is a combination of an Entry and a listbox control, which means that a user can either type the value they want to select in the dropdown or choose a value from a pre-defined list.
Creating a Combobox Widget in Python Tkinter
To create a Combobox widget in Tkinter, we first need to import the Combobox module from the tkinter library. We also need to import the ttk module, which contains more modern and themed Tkinter widgets.
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("200x200")
cmb = ttk.Combobox(root)
cmb.pack()
root.mainloop()
In the code example above, we created a Combobox widget and placed it on the GUI application. We gave the Combobox widget an instance name of cmb, added it to the application using the pack() layout manager, and ran the main event loop using the root.mainloop()
method.
Adding Values to Combobox Dynamically
The Combobox widget is more useful when it is populated with data dynamically. For instance, a user might want to select the country of residence from a pre-defined list of countries. In Python Tkinter, we can add data to a Combobox widget using the ['value']
method. Let’s update the code example above to add some data.
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("200x200")
# adding sample options
options = ['USA', 'UK', 'Canada', 'Australia']
cmb = ttk.Combobox(root, values=options)
cmb.pack()
root.mainloop()
In the updated code example above, we added a list of country options to the Combobox widget using the values
parameter. The options are defined in a list data type named options
. We have four different sample country options that the user can select.
Fetching Selected Value from the Combobox
After adding values to the Combobox widget, we may also need to get or fetch the selected value from the Combobox widget. We can achieve this using the current()
method. The current()
method returns the index of the currently selected value from the Combobox widget.
Let’s update the code example above to fetch the selected value.
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("200x200")
# adding sample options
options = ['USA', 'UK', 'Canada', 'Australia']
cmb = ttk.Combobox(root, values=options)
cmb.pack()
def get_selected_value(event):
print(cmb.get())
# bind a function to the combobox
cmb.bind('<<ComboboxSelected>>', get_selected_value)
root.mainloop()
In the updated code example above, we added a new function named get_selected_value
, which will print the selected value of the Combobox widget. We bind the function to the <<ComboboxSelected>>
event, which happens every time a user selects an option from the Combobox widget.
Styling a Combobox Widget
Tkinter Combobox widget resembles other default UI controls from other toolkits, such as Windows, Mac, and Unix. However, you can customize it using different styling options such as font, size, color, and background. We can do this using the configure()
method of Tkinter.
Let’s update the code example above to style the Combobox.
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("200x200")
# adding sample options
options = ['USA', 'UK', 'Canada', 'Australia']
combostyle = ttk.Style()
combostyle.theme_create('combostyle', parent='alt',
settings={'TCombobox':
{'configure':
{'foreground': 'red',
'background': 'yellow',
'fieldbackground': 'yellow',
'font': ('Arial', 10)}}
})
combostyle.theme_use('combostyle')
cmb = ttk.Combobox(root, values=options, style="TCombobox")
cmb.pack()
def get_selected_value(event):
print(cmb.get())
# bind a function to the combobox
cmb.bind('<<ComboboxSelected>>', get_selected_value)
rootIn the updated code example above, we created and applied a custom style to the Combobox widget. We created a new instance of ttk.Style()
and named it combostyle
. We then created a dictionary with different configuration options such as foreground
, background
, fieldbackground
and font
. The dictionary was then applied to the TCombobox
setting of the Combobox widget.
We also changed the default background of the Combobox widget to yellow and the foreground to red, to make it standout. To implement our new custom style, we called on combostyle.theme_use('combostyle')
.
Conclusion
In conclusion, creating and using a Combobox widget in Tkinter is relatively easy. We can populate them dynamically with data and style them according to our preferences. The Combobox widget provides a user-friendly interface that allows users to select options easily. We hope this article has helped you understand how to use the Combobox widget in Tkinter and how to add data to it dynamically.