my_conn as the same is used inside rest of the script.
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
try: # connection to database
my_path="D:\\testing\\sqlite\\my_db.db" #Change the path
my_conn = create_engine("sqlite:///" + my_path) # for SQLite database
# for MySQL use below line #
#my_conn =create_engine("mysql+mysqldb://root:pw@localhost/my_database")
except SQLAlchemyError as e:
error = str(e.__dict__['orig'])
print(error)
#query='SHOW TABLES' # for MySQL
query = "SELECT name FROM sqlite_schema WHERE type = 'table'" # SQLite
#query= "SELECT * FROM INFORMATION_SCHEMA.TABLES" # MSSQL
my_data=my_conn.execute(query) # SQLAlchemy my_conn result
tables = [row[0] for row in my_data] # list of tables
The list tables we will use as options for our Combobox
import tkinter as tk
from tkinter import *
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("500x400")
sel=tk.StringVar() # string variable
cb1 = ttk.Combobox(my_w, values=tables,textvariable=sel) # Combobox
cb1.grid(row=0,column=0,padx=10,pady=20) # adding to grid
#sel.trace('w',my_show) # triger the function on change of StringVar
my_w.mainloop() # keep the window open
t1 = tk.Text(my_w, height=7, width=45,bg='yellow') # added one text box
t1.grid(row=1,column=0,padx=10,pady=10) #
We can delete or add data to the text widget. Here my_str is the data we want to add or display inside the text widget t1.
t1.delete('1.0',END) # Delete all previous data if any
t1.insert(tk.END, my_str) # add data
Inside the function my_show() we created the Query by using the selected option of cb1 ( table name ) and we used LIMIT query to get 5 records. def my_show(*args):
query="SELECT * FROM " + sel.get() +" LIMIT 0,5"
my_data=my_conn.execute(query) # SQLAlchemy my_conn result
data=[row for row in my_data] # crate a list from data
my_str="\n".join(map(str,data)) # create a string
t1.delete('1.0',END) # Delete all previous data if any
t1.insert(tk.END, my_str) # add data
sel.trace('w',my_show) # trigger the function on change of StringVar sel
Full code is here.
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
try: # connection to database
my_path="D:\\testing\\sqlite\\my_db.db" #Change the path
my_conn = create_engine("sqlite:///" + my_path)
# for MySQL use below line #
#my_conn =create_engine("mysql+mysqldb://root:pw@localhost/my_database")
except SQLAlchemyError as e:
error = str(e.__dict__['orig'])
print(error)
#query='SHOW TABLES' # for MySQL
query = "SELECT name FROM sqlite_schema WHERE type = 'table'" # SQLite
#query= "SELECT * FROM INFORMATION_SCHEMA.TABLES" # MSSQL
my_data=my_conn.execute(query) # SQLAlchemy my_conn result
tables = [row[0] for row in my_data] # list of tables
import tkinter as tk
from tkinter import *
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("500x400")
sel=tk.StringVar() # string variable
cb1 = ttk.Combobox(my_w, values=tables,textvariable=sel) # Combobox
cb1.grid(row=0,column=0,padx=10,pady=20) # adding to grid
t1 = tk.Text(my_w, height=7, width=45,bg='yellow') # added one text box
t1.grid(row=1,column=0,padx=10,pady=10) #
def my_show(*args):
query="SELECT * FROM " + sel.get() +" LIMIT 0,5"
my_data=my_conn.execute(query) # SQLAlchemy my_conn result
data=[row for row in my_data] # crate a list from data
my_str="\n".join(map(str,data)) # create a string
t1.delete('1.0',END) # Delete from position 0 till end
t1.insert(tk.END, my_str) # add data
sel.trace('w',my_show) # triger the function on change of StringVar
my_w.mainloop() # keep the window open
Combobox
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.