Here in this coding we are going to see the entry attached with listbox and database of sqlite 3
so here is the coding to as follows
========================================================================
from tkinter import *
import sqlite3
root = Tk()
root.title('Codemy.com - Auto Select/Search')
root.geometry("500x300")
# Update the listbox
def update(data):
# Clear the listbox
my_list.delete(0, END)
# Add toppings to listbox
for item in data:
my_list.insert(END, item)
# Update entry box with listbox clicked
def fillout(e):
# Delete whatever is in the entry box
my_entry.delete(0, END)
# Add clicked list item to entry box
my_entry.insert(0, my_list.get(ANCHOR))
my_list.forget()#here forget will work because re-entry can be done by deleting
the items.
# Create function to check entry vs listbox
def check(e):
#grab what was typed
typed = str(my_entry.get())
my_list.pack()
if typed == '':
data = toppings
else:
data = []
for item in toppings:
if typed.lower() in item.lower():
data.append(item)
# update our listbox with selected items
update(data)
# Create a label
my_label = Label(root, text="Start Typing...",
font=("Helvetica", 14), fg="grey")
my_label.pack(pady=20)
# Create an entry box
my_entry = Entry(root, font=("Helvetica", 20))
my_entry.pack()
# Create a listbox
my_list = Listbox(root, width=50)
# Create a list of pizza toppings
conn = sqlite3.connect('society_data.db')
conn.row_factory = lambda cursor, row: row[1]
c = conn.cursor()
c.execute("SELECT code,dcs_name FROM society_data")
rows = c.fetchall()
# load the listbox with data
for item in rows:
my_list.insert(END, str(item).replace("(", "").replace(")", "").replace("'", ""))
toppings=rows
# Add the toppings to our rows
update(toppings)
# Create a binding on the listbox onclick
my_list.bind("<<ListboxSelect>>", fillout)
# Create a binding on the entry box
my_entry.bind("<KeyRelease>", check)
root.mainloop()
Comments
Post a Comment