
Part I | Part II |
|---|---|
Create a list by using data from MySQL student table | Create an OptionMenu by using the elements of the list to display the options |
query="SELECT distinct(class) as class FROM student"
Getting recordsmy_data=my_conn.execute(text(query)) # SQLAlchem engine result set
Using this reselt 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 OptionaMenu.
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
We will create a StringVar() and set the default value for the optionMenu.
options = tk.StringVar(my_w)
options.set(my_list[0]) # default value
Set the optionMenu and add the option values
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=2,column=5)
Show the window
my_w.mainloop()
Full code is here from sqlalchemy import create_engine,text
my_conn = create_engine("mysql+mysqldb://id:pw@localhost/my_db")
#path="sqlite:///C:\\testing\\my_db.db" # SQLite database
#my_conn = create_engine(path)
my_conn=my_conn.connect()
query="SELECT DISTINCT(class) as class FROM student"
my_data=my_conn.execute(text(query))
my_list = [r for r, in my_data] # create a list
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
options = tk.StringVar(my_w)
options.set(my_list[0]) # default value
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=2,column=5)
my_w.mainloop()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.