Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
The Combobox
widget is a combination of a drop-down list and an input field. In tkinter
, the Combobox
widget is available through the ttk
module, which provides access to the Tk themed widget set.
Here's a step-by-step tutorial to guide you on using the Combobox
widget in tkinter
:
Setting up the basic GUI with Tkinter:
Start with a basic window using tkinter
.
import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Combobox in Tkinter") root.geometry("300x200")
Adding the Combobox widget:
Use the ttk.Combobox
to create the combobox widget.
# Create a label label = ttk.Label(root, text="Choose a country:") label.pack(pady=10) # Create the Combobox countries = ["USA", "Canada", "Australia", "India", "UK", "Germany"] country_combobox = ttk.Combobox(root, values=countries) country_combobox.pack(pady=10)
Setting and getting values from Combobox:
To set a default value:
country_combobox.set("USA")
To retrieve the currently selected value:
def get_selected_country(): selected_country = country_combobox.get() print(f"Selected Country: {selected_country}") # Button to trigger value retrieval btn = ttk.Button(root, text="Get Selected Country", command=get_selected_country) btn.pack(pady=20)
Run the main loop:
root.mainloop()
Combining the code snippets above and running the script will present a tkinter
window with a Combobox
widget. The Combobox
will have a list of countries. By clicking the "Get Selected Country" button, the selected country's name will be printed in the console.
The Combobox
also provides additional functionalities such as:
state
option to make it read-only.<<ComboboxSelected>>
to execute a function whenever a new value is selected from the drop-down list.To implement a read-only Combobox
, you can use:
country_combobox['state'] = 'readonly'
To bind to the selection event:
def on_value_change(event): selected_country = country_combobox.get() print(f"Selected Country: {selected_country}") country_combobox.bind("<<ComboboxSelected>>", on_value_change)
You can incorporate these additional functionalities based on the needs of your application.
How to use ComboBox in Tkinter:
import tkinter as tk from tkinter import ttk def on_select(event): selected_value = combo.get() print(f"Selected Value: {selected_value}") root = tk.Tk() # Create a ComboBox options = ["Option 1", "Option 2", "Option 3"] combo = ttk.Combobox(root, values=options) combo.grid(row=0, column=0, padx=10, pady=10) combo.bind("<<ComboboxSelected>>", on_select) root.mainloop()
Creating dropdown menus with Tkinter ComboBox:
import tkinter as tk from tkinter import ttk root = tk.Tk() # Create a ComboBox for a dropdown menu options = ["Option 1", "Option 2", "Option 3"] combo = ttk.Combobox(root, values=options) combo.grid(row=0, column=0, padx=10, pady=10) root.mainloop()
Customizing options in Tkinter ComboBox:
import tkinter as tk from tkinter import ttk root = tk.Tk() # Customize ComboBox options combo = ttk.Combobox(root, values=["Option 1", "Option 2", "Option 3"], font=("Arial", 12), state="readonly") combo.grid(row=0, column=0, padx=10, pady=10) root.mainloop()
Handling events with Tkinter ComboBox:
bind
method.import tkinter as tk from tkinter import ttk def on_select(event): selected_value = combo.get() print(f"Selected Value: {selected_value}") root = tk.Tk() # Handle events with ComboBox combo = ttk.Combobox(root, values=["Option 1", "Option 2", "Option 3"]) combo.grid(row=0, column=0, padx=10, pady=10) combo.bind("<<ComboboxSelected>>", on_select) root.mainloop()
Adding items dynamically to Tkinter ComboBox:
import tkinter as tk from tkinter import ttk def add_item(): new_item = entry.get() combo['values'] = combo['values'] + (new_item,) root = tk.Tk() # Add items dynamically to ComboBox combo = ttk.Combobox(root, values=["Option 1", "Option 2", "Option 3"]) combo.grid(row=0, column=0, padx=10, pady=10) entry = tk.Entry(root) entry.grid(row=1, column=0, padx=10, pady=10) add_button = tk.Button(root, text="Add Item", command=add_item) add_button.grid(row=1, column=1, padx=10, pady=10) root.mainloop()
Tkinter ComboBox with default values:
import tkinter as tk from tkinter import ttk root = tk.Tk() # Set default value for ComboBox combo = ttk.Combobox(root, values=["Option 1", "Option 2", "Option 3"], state="readonly") combo.set("Option 1") combo.grid(row=0, column=0, padx=10, pady=10) root.mainloop()
Bind function to ComboBox selection in Tkinter:
import tkinter as tk from tkinter import ttk def on_select(event): selected_value = combo.get() print(f"Selected Value: {selected_value}") root = tk.Tk() # Bind function to ComboBox selection combo = ttk.Combobox(root, values=["Option 1", "Option 2", "Option 3"]) combo.grid(row=0, column=0, padx=10, pady=10) combo.bind("<<ComboboxSelected>>", on_select) root.mainloop()
Setting width and height of Tkinter ComboBox:
import tkinter as tk from tkinter import ttk root = tk.Tk() # Set width and height for ComboBox combo = ttk.Combobox(root, values=["Option 1", "Option 2", "Option 3"], width=15, height=5, state="readonly") combo.grid(row=0, column=0, padx=10, pady=10) root.mainloop()
Working with ComboBox in Tkinter forms:
import tkinter as tk from tkinter import ttk def submit_form(): selected_option = combo.get() print(f"Form submitted with option: {selected_option}") root = tk.Tk() # ComboBox in a form combo = ttk.Combobox(root, values=["Option 1", "Option 2", "Option 3"], state="readonly") combo.grid(row=0, column=0, padx=10, pady=10) submit_button = tk.Button(root, text="Submit", command=submit_form) submit_button.grid(row=1, column=0, pady=10) root.mainloop()