Interlinked Combobox

Tkinter Combobox Combobox Options from Database, CSV , Json

Two inter linked Comboboxes
From our student table we will collect unique student classes and use them as first options of Combobox.

Tkinter two interlinked Comboboxes where options of second is taken based on Selection of first

Read the tutorial on how to add options to Combobox by using data from MySQL or SQLite or Json or CSV file.
The first Combobox displaying classes. On Select of any option of the first Combobox, the String variable Sel triggers the function my_upd()
sel.trace('w',my_upd) # track the change event 
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

Linking Three Comboboxes

Three linked Comboboxes
To the above code we will include one more Combobox (total three) to filter the student names who has got more than or equal to 60 ( Pass ) and those who got less than 60 ( Fail ).

To check the result we will include Mark with the name of the student in our final ( 3rd here ) drop down combobox. We joined student name and mark and return the column using CONCAT().
Here is a sample query using CONCAT().
query="SELECT CONCAT(name,'-',mark)  FROM student \
	WHERE class='"+sel.get()+"'"
Here the main change is updating the Query ( SQL ) to filter the records based on the selection of option.
Based on the user selection of option ( All, Pass , Fail ) from the Combobox the query is prepared.
    if(sel1.get() =='All'):
        query="SELECT CONCAT(name,'-',mark)  FROM student \
             WHERE class='"+sel.get()+"'"
    elif(sel1.get()=='Pass'):
        query="SELECT concat(name,'-',mark)  FROM student \
            WHERE class='"+sel.get()+"' AND mark >= 60"
    else:
        query="SELECT concat(name,'-',mark)  FROM student \
            WHERE class='"+sel.get()+"' AND mark < 60"
To triggering the function to update the third list we will use trace() method of second drop down Combo box. Here sel1 is the textvariable connected to 2nd combobox.
sel1.trace('w',my_upd) # track the change event of second list

Tkinter Three interlined comboboxes filtering records based on selected options of users.


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 using class

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
my_list2=[] 
def my_upd(*args):
    cb2.set('') # remove the previous selected option 
    if(sel1.get() =='All'):
        query="SELECT CONCAT(name,'-',mark)  FROM student \
             WHERE class='"+sel.get()+"'"
    elif(sel1.get()=='Pass'):
        query="SELECT concat(name,'-',mark)  FROM student \
            WHERE class='"+sel.get()+"' AND mark >= 60"
    else:
        query="SELECT concat(name,'-',mark)  FROM student \
            WHERE class='"+sel.get()+"' AND mark < 60"

    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=5,pady=30)

my_list1=['All','Pass','Fail'] # Options for the second combobox
sel1=tk.StringVar(value='All') # variable to store the selection
cb1_a=ttk.Combobox(my_w,values=my_list1,textvariable=sel1,width=5)
cb1_a.grid(row=1,column=2,padx=5)

cb2 = ttk.Combobox(my_w, values=my_list2,width=15)
cb2.grid(row=1,column=3)

sel.trace('w',my_upd) # track the change event of first list
sel1.trace('w',my_upd) # track the change event of second list
my_w.mainloop()  # Keep the window open

Using SQLite Database

In above code only the connection part will be changed. Download or create the SQLite database with sample data from here. The connection object my_conn is to be created by using SQLite database, so in above code this the 3rd line is to be changed.
For MySQL
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/db_name")
For SQLite ( Change the path )
my_conn = create_engine("sqlite:///D:\\testing\\sqlite\\my_db.db")
Combobox Combobox Options from MySQL, SQLite, Json or CSV files
Listbox OptionMenu
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer