Inside the function my_upd() we will first clear the previous selection if any for the second Combobox cb2. Then run the query to get the name list from the student table by using the selected option of first Combobox ( sel.get() ). The output is stored in the list my_list2 and the same is used as option value of the second Combobox cb2 ( cb2['values']=my_list2 )
def my_upd(*args):
cb2.set('') # remove the previous selected option
query="SELECT name FROM student WHERE class='"+sel.get()+"'"
my_data=my_conn.execute(query) # SQLAlchem engine result
my_list2 = [r for r, in my_data] # create a list
cb2['values']=my_list2
Full code is here
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://root:test@localhost/my_tutorial")
#my_conn = create_engine("sqlite:///D:\\testing\\sqlite\\my_db.db")
query="SELECT distinct(class) as class FROM student"
my_data=my_conn.execute(query) # SQLAlchem engine result
my_list = [r for r, in my_data] # create a list
import tkinter as tk
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("300x150") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_list2=[]
def my_upd(*args):
cb2.set('') # remove the previous selected option
query="SELECT name FROM student WHERE class='"+sel.get()+"'"
my_data=my_conn.execute(query) # SQLAlchem engine result
my_list2 = [r for r, in my_data] # create a list
cb2['values']=my_list2
sel=tk.StringVar()
cb1 = ttk.Combobox(my_w, values=my_list,width=15,textvariable=sel)
cb1.grid(row=1,column=1,padx=30,pady=30)
sel.trace('w',my_upd) # track the change event
cb2 = ttk.Combobox(my_w, values=my_list2,width=15)
cb2.grid(row=1,column=2)
my_w.mainloop() # Keep the window open