In this project, we connect a Tkinter OptionMenu to an SQLite database so that the dropdown dynamically loads and updates data entries. It demonstrates how to bridge GUI elements with real database operations for practical applications.

Part I | Part II |
|---|---|
Create a list by using data from SQLite student table | Create an OptionMenu by using the elements of the list to display the options |
query="SELECT distinct(class) as class FROM student"
Getting recordsquery="SELECT distinct(class) as class FROM student"
r_set=my_conn.execute(query);
Using this reselt set (r_set) of SQLite we will create one list.
my_list = [r for r, in r_set] # create a list
We will connect my_list to our OptionaMenu.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
We will create a StringVar() and set the default value for the optionMenu.
options = tk.StringVar(my_w)
options.set(my_list[0]) # default value
Set the optionMenu and add the option values
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=2,column=5)
Show the window
my_w.mainloop()
Full code is here import sqlite3
my_conn = sqlite3.connect('my_db.db')
###### end of connection ####
query="SELECT distinct(class) as class FROM student"
r_set=my_conn.execute(query);
my_list = [r for r, in r_set] # create a list
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
options = tk.StringVar(my_w)
options.set(my_list[0]) # default value
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=2,column=5)
my_w.mainloop()
from sqlalchemy import create_engine,text
#my_conn = create_engine("mysql+mysqldb://id:pw@localhost/my_db") # MySQL
path="sqlite:///C:\\testing\\my_db.db" # SQLite database , update path
my_conn = create_engine(path)
my_conn=my_conn.connect()
query="SELECT DISTINCT(class) as class FROM student"
my_data=my_conn.execute(text(query))
my_list=[r for r , in my_data] # create a list of
#for row in my_data:
# print(row)
#my_list=['One','Two','Three','Four']
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x300") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
options = tk.StringVar(my_w)
options.set(my_list[1]) # default value
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=2,column=5,padx=100,pady=10)
menu=my_w.nametowidget(om1.menuname)
om1.config(font=['Arial',26]) # Set the menu font
menu.config(font=['Arial',16]) # Set option font
my_w.mainloop()
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.