Skip to main content

entry with listbox and database

 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

Popular posts from this blog

How to add the treeview numbers to get the grand total in tkinter entry box

 in this blog we are going to know about the adding of the integers in the treeview and get the total in the entry box of tkinter so here is the image first so the coding to do such things are here as follow but here is the condition that pls make assured that the state of entry box is disabled.......      for child in my_tree1 . get_children ():         e2l . config ( state = "normal" )         b1 += float ( my_tree1 . item ( child , "values" )[ 3 ])         var2 . set ( b1 )         e2l . config ( state = "disable" )      l2l = Label ( ento , text = "Purnea-2" , width = 8 )      l2l . grid ( row = 0 , column = 1 )     e2l = Entry ( ento , textvariable = var2 , state = "disable" , width = 8 )     e2l . grid ( row = 1 , column = 1 )