OptionMenu option values from SQLite table

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

Part 1 : Creating a list using data from SQLite student table.

We will first establish connection to SQLite database .

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 Sqlite 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.
query="SELECT distinct(class) as class FROM student"
r_set=my_conn.execute(query);
Using this reselt set (r_set) of SQLite we will create one list.
my_list = [r for r, in r_set] # 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

import sqlite3
my_conn = sqlite3.connect('my_db.db')
###### end of connection ####

query="SELECT distinct(class) as class FROM student"
r_set=my_conn.execute(query);
my_list = [r for r, in r_set] # 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 SQLite table and created one list using the data. In part 2 we created the OptionMenu and connected the data list as option values.

View & Download tkinter-optionmenu-proj3-sqlite ipynb file (.html format)

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 MySQL database table


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer