from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/my_database")
Using SQLite
import sqlite3
my_conn = sqlite3.connect('my_db.db')
Download or Create my_db.db SQLite Database
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
*my_dict1.values()
. We used * to unpack the list of values of dictinary and return as items.
query="SELECT cat_id,category FROM category" # Query
r_set=my_conn.execute(query);
my_dict1=dict((x,y) for x,y in r_set) # create dictionary
options1 = tk.StringVar(my_w)
options1.set(my_dict1[1]) # default value
om1 =tk.OptionMenu(my_w, options1, *my_dict1.values())
om1.grid(row=2,column=5)
options2 = tk.StringVar(my_w)
my_list2=['Subcategory list']
options2.set(my_list2[0]) # default value
om2 =tk.OptionMenu(my_w, options2, *my_list2)
om2.grid(row=2,column=6)
options1.trace('w',my_show)
to trigger the function my_show(*args) whenever the value of StringVar options1 changes. So whenever we change the selection of om1, the function my_show(*args) is executed.
def my_show(*args):
for i,j in my_dict1.items():
if j==options1.get():
cat_id=i
--------------
--------------
options1.trace('w',my_show)
In above code, inside the function my_show(*args), we are first reading the vlaue of user selection of OptionMenu om1. This value we will get by using options.get()
. Using this value of selected element we will collect the corresponding cat_id. So our variable cat_id will store the cat_id value of user selected category of the OptionMenu om1.
options2.set('') # remove default selection only
om2['menu'].delete(0,'end') # remove full list
query="SELECT subcategory FROM subcategory WHERE cat_id='"+str(cat_id)+"'"
r_set2=my_conn.execute(query);
my_list2 = [r for r, in r_set2] # create a list
for opt in my_list2:
om2['menu'].add_command(label=opt, command=tk._setit(options2, opt))
options2.set(my_list2[0]) # default value set
Here is the full code.
# www.plus2net.com
# download updated script at https://www.plus2net.com/python/tkinter-OptionMenu-proj4.php
import sqlite3
my_conn = sqlite3.connect('my_db.db')
###### end of connection ####
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("200x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
query="SELECT cat_id,category FROM category"
r_set=my_conn.execute(query);
my_dict1=dict((x,y) for x,y in r_set)
#print(my_dict1)
options1 = tk.StringVar(my_w)
#options.set(my_dict1[0]) # default value
om1 =tk.OptionMenu(my_w, options1, *my_dict1.values())
om1.grid(row=2,column=5)
options2 = tk.StringVar(my_w)
my_list2=['Subcategory list']
options2.set(my_list2[0])
om2 =tk.OptionMenu(my_w, options2, *my_list2)
om2.grid(row=2,column=6)
def my_show(*args):
for i,j in my_dict1.items():
if j==options1.get():
cat_id=i
options2.set('') # remove default selection only, not the full list
om2['menu'].delete(0,'end') # remove full list
query="SELECT subcategory FROM subcategory WHERE cat_id='"+str(cat_id)+"'"
r_set2=my_conn.execute(query);
my_list2 = [r for r, in r_set2] # create a list
#print(my_list2)
for opt in my_list2:
om2['menu'].add_command(label=opt, command=tk._setit(options2, opt))
options2.set(my_list2[0]) # default value set
options1.trace('w',my_show)
my_w.mainloop()
View & Download tkinter-optionmenu-proj4 ipynb file (.html format)
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.