In this TKinter Tutorial we are going to learn about TKinter ComboBox, so first of all let’s talk talk that What is TKinter ComboBox?
What is TKinter ComboBox?
Tkinter ComboBox, also known as a dropdown list or a combobox, is a user interface element that allows users to select an item from a list of predefined options. It combines the features of a text entry field and a listbox, and it provides users to choose from a set of choices.
In Tkinter ComboBox is implemented using ttk.Combobox widget from the ttk module, which stands for “themed Tkinter.” This widget provides more modern look and feel compared to the traditional Tkinter widgets. TKinter ComboBox widget typically consists of an entry field where users can type text or select an option from the dropdown list by clicking on a small arrow button. When the arrow button is clicked, the dropdown list expands to display the available options, and users can select one of them.
This is the complete source code for TKinter ComboBox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import tkinter as tk from tkinter import ttk # Create main window window = tk.Tk() # Set minimum size for window window.minsize(600, 400) # Set window title window.title("Codeloop - Tkinter Combobox") # Set window icon window.iconphoto(True, tk.PhotoImage(file='codeloop.png')) # Function to display selected number def choosingNumbers(): # Update the label text with the selected number label.configure(text="You Have Chosen " + mynumber.get()) # Label widget to prompt the user to choose a number label = ttk.Label(window, text="Choose A Number") label.grid(column=0, row=0) # Variable to store the selected number mynumber = tk.StringVar() # Combobox widget to display a dropdown list of numbers combobox = ttk.Combobox(window, width=15, textvariable=mynumber) combobox['values'] = (5, 6, 7, 8, 9, 10, 12, 14, 15) # Set the values for the dropdown list combobox.grid(column=1, row=0) # Button to trigger the selection button = ttk.Button(window, text="Click Me", command=choosingNumbers) button.grid(column=1, row=1) # Start the Tkinter event loop window.mainloop() |
This line of code is for creating object of TKinter window.
1 |
window = tk.Tk() |
In these lines of codes we set the size of our window, also title with icon. make sure that have an icon in your project directory.
1 2 3 |
window.minsize(600, 400) window.title("Codeloop - Tkinter Combobox") window.iconphoto(True, tk.PhotoImage(file='codeloop.png')) |
This method is for click events that we are going to handle, the click events of button and you will see that we connect this method to our button, also we have created a label.
1 2 3 4 5 |
def chosingNumbers(): label.configure(text = "You Have Choosed " + mynumber.get()) label = ttk.Label(window, text = "Choose A Number") label.grid(column = 0, row = 0) |
This line of code is for getting the entry from the user.
1 |
name = tk.StringVar() |
These lines of codes are for creating ComboBox in Python TKinter.
1 2 3 |
combobox = ttk.Combobox(window, width = 15 , textvariable = mynumber) combobox['values'] = (5,6,7,8,9,10,12,14,15) combobox.grid(column = 1, row = 0) |
Also we have created a button because we want to handle the click events. and if you remember, now we connect our def chosingNumbers() method in here.
1 2 |
button = ttk.Button(window, text = "Click Me", command = chosingNumbers) button.grid(column = 1, row = 1) |
Run the complete code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email