import tkinter as tk
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("400x150") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
from sqlalchemy import create_engine
from sqlalchemy.sql import text
my_conn=create_engine("mysql+mysqldb://userid:password@localhost/db_name")
query = "SELECT id,name FROM student" # SQL
my_data = list(my_conn.execute(query))
my_dict = {}
p_id, p_name = 0, ""
for row in my_data:
my_dict[row[0]] = row[1]
Using the above dictionary my_dict, values we will populate in the Combobox cb1, check the values parameter.
sel = tk.StringVar() # string variable
cb1 = ttk.Combobox(my_w, values=list(my_dict.values()),
width=15, textvariable=sel)
cb1.grid(row=1, column=1, padx=5, pady=20)
As we can use Combobox as entry widget by entering data other than the given options ( that’s why the name Combobox ) , we will trigger a function my_upd() using the trace method of StringVar() sel.
sel.trace("w", my_upd) # trigger the function on change of sel
Inside the function my_upd() we will create the SQL and get the records from table. Note that while creating the query we will use the user input by taking cb1.get() value.
def my_upd(*args):
my_dict = {}
query = text("SELECT id,name FROM student WHERE name like :e1")
#print(query)
my_data = list(my_conn.execute(query, e1="%" + cb1.get() + "%"))
for row in my_data:
my_dict[row[0]] = row[1]
#print(my_data)
cb1["values"] = [] # remove all previous options
cb1["values"] = list(my_dict.values()) # add new options
l1.config(text="Number records : " + str(len(my_data)))
Based on the string what the user has entered we are displaying the number of records found using our Label l1. Once the user select one of the option and moves out ( FocusOut ) , we will trigger the my_disp() function.
def my_disp(*args):
global p_id, p_name
l1.config(text="") # remove the text from Label
for i, j in my_dict.items(): # all key and values of the dictionary
if j == sel.get(): # matching value
p_id, p_name = i, j # set key and value
l1.config(text=" Student ID: " + str(i) + " \n Name: " + j)
break # come out of the for loop
import tkinter as tk
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("400x150") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
from sqlalchemy import create_engine
from sqlalchemy.sql import text
my_conn = create_engine("mysql+mysqldb://id:pw@localhost/my_db")
query = "SELECT id,name FROM student"
my_data = list(my_conn.execute(query))
my_dict = {}
p_id, p_name = 0, ""
for row in my_data:
my_dict[row[0]] = row[1]
def my_upd(*args):
my_dict = {}
query = text("SELECT id,name FROM student WHERE name like :e1")
#print(query)
my_data = list(my_conn.execute(query, e1="%" + cb1.get() + "%"))
for row in my_data:
my_dict[row[0]] = row[1]
#print(my_data)
cb1["values"] = [] # remove all previous options
cb1["values"] = list(my_dict.values()) # add new options
l1.config(text="Number records : " + str(len(my_data)))
def my_disp(*args):
global p_id, p_name
l1.config(text="") # remove the text from Label
for i, j in my_dict.items(): # all key and values of the dictionary
if j == sel.get(): # matching value
p_id, p_name = i, j # set key and value
l1.config(text=" Student ID: " + str(i) + " \n Name: " + j)
break # come out of the for loop
sel = tk.StringVar() # string variable
cb1 = ttk.Combobox(my_w, values=list(my_dict.values()),textvariable=sel)
cb1.grid(row=1, column=1, padx=5, pady=20)
l1 = tk.Label(my_w, text="Result", bg="yellow", width=25,font=20)
l1.grid(row=1, column=2)
e1 = tk.Entry(my_w, text="Result", bg="yellow", width=15)
e1.grid(row=2, column=1)
sel.trace("w", my_upd) # trigger the function on change of sel
cb1.bind("<FocusOut>", my_disp) # once focus is out of the Combobox
my_w.mainloop() # Keep the window open
Combobox
Combobox Options from MySQL, SQLite, Json or CSV files Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.