Combobox data source from MySQL,SQLite,CSV & Json

Tkinter Combobox Widget in Tkinter Two Linked Comboboxes

Combobox options from MySQL table

Tkinter Combobox options taken from MySQL, SQLite database table, CSV file and Json string
In our student table we will collect unique student classes and use them as options of Combobox.
The script will have two parts.

Part 1 : Creating a list using data from MySQL student table.

We will first establish connection to MySQL database by using sqlalchemy. You can download and use the sql dump of student table here.

The query.
To get the unique class names from the student table we will use DISTINCT sql. Here is the Query we used.
query="SELECT distinct(class) as class FROM student"
Getting records
We will get sqlalchemy result set with data by using the above query. We will convert this result set to a list as we are planning to use as options of OptionMenu.
my_data=engine.execute(query) # SQLAlchem engine result set 
Using this result set of SQLalchem we will create one list.
my_list = [r for r, in my_data] # create a  list 
We will connect my_list to our Combobox.

Part 2 : Creating Combobox using the elements of the list

We will create the window first
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
Set the Combobox and add the option values
cb1 = ttk.Combobox(my_w, values=my_list,width=15)
cb1.grid(row=1,column=1,padx=30,pady=30)
Show the window
my_w.mainloop() 
Full code is here ( Update your MySQL id , pw and db_name )
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://id:pw@localhost/db_name")
#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

cb1 = ttk.Combobox(my_w, values=my_list,width=15)
cb1.grid(row=1,column=1,padx=30,pady=30)
cb1.current(2) # default selected option
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")

Using JSON to add options to Combobox

Here is the sample JSON file.
We used append method to add elements to the list.
import tkinter as tk
from tkinter import ttk
import json
my_w = tk.Tk()
my_w.geometry("300x150")  # Size of the window 
my_w.title("www.plus2net.com")  # Adding a title

path="D:\\my_data\\student.json" # sample json file, use your path 
fob=open(path,)
data=json.load(fob)
names=[]
for student in data:
    names.append(student['name'])
	
cb1 = ttk.Combobox(my_w,values=names)
cb1.grid(row=1,column=1,padx=10,pady=20)
cb1.current(2) # default selected option

fob.close() # close the file pointer 
my_w.mainloop()  # Keep the window open

Using CSV file

ttk Combobox options from CSV file
We can use one Comma Separated Values( CSV ) file and use one of the column as options for a combobox.
Download the sample student CSV file from here
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
names=[]
path="D:\\my_data\\student.csv" # sample CSV file, use your path
fob=open(path,)
headings = next(fob) # removing header row
for rec in fob:
  student=rec.split(',')
  print(student)
  names.append(student[1]) # name added

cb1 = ttk.Combobox(my_w,values=names)
cb1.grid(row=1,column=1,padx=10,pady=20)

fob.close() # close the file pointer 
my_w.mainloop()  # Keep the window open
Combobox Two interlinked Comboboxes
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