Part I | Part II |
---|---|
Create a list by using data from MySQL student table | Create an Spinbox by using the elements of the list to display the options |
query="SELECT distinct(class) as class FROM student"
Getting recordsmy_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 Spinbox.
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 Spinbox and add the option values
font1=('Times',24,'bold')
cb1 = tk.Spinbox(my_w, values=my_list,width=10,font=font1)
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
my_w = tk.Tk()
my_w.geometry("300x150") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
font1=('Times',24,'bold')
cb1 = tk.Spinbox(my_w, values=my_list,width=10,font=font1)
cb1.grid(row=1,column=1,padx=30,pady=30)
my_w.mainloop() # Keep the window open
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")
import tkinter as tk
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'])
font1=('Times',24,'bold')
cb1 = tk.Spinbox(my_w,values=names,font=font1,width=10)
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
import tkinter as tk
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 = tk.Spinbox(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
Spinbox