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

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

* 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-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer