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=engine.execute(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
engine = create_engine("mysql+mysqldb://userid:password@localhost/my_database")
query="SELECT distinct(class) as class FROM student"
my_data=engine.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("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()