OptionMenu option values from MySQL table


OptionMenu with option values from MySQL database
In our student table we will collect unique student classes and use them as option of OptionaMenu.
The script will have two parts.


Tkinter OptionMenu options from MySQL or SQLite Database tables for user selection

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=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.

Part 2 : Creating OptionMenu 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
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

Update your MySQL login details ( userid, password and my_database )
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() 

Conclusion

We have collected data from MySQL table and created one list using the data. In part 2 we created the OptionMenu and connected the data list as option values.

Using this concepts we can further develop applications where OptionMenu is used. Remove all or remove Selective options of an OptionMenu.
OptionMenu
Create one OptionMenu with option values taken from SQLite database table Combobox options from MySQL table
Subscribe to our YouTube Channel here


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-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer