Skip to main content

Auto list with entry box and database and even the concatenation of data

 Here in this blog, we are having some objectives which you will see here pointwise

  1. create Tkinter
  2. then create two tabs 
  3. in a second tab, I have created a database entry system and I have also concatenated the two databases into one.
  4. And after that we have called the concatenated item in the list box and then automated the Listbox
==========================================================================

                                                                                    
========================================================================
And here is the following code
from tkinter import *
import sqlite3
from tkinter import ttk
root=Tk()
root.geometry("400x500")
root.title("auto society")

conn = sqlite3.connect("society_sample.db")
c = conn.cursor()
# create table
c.execute(
    """CREATE TABLE IF NOT EXISTS sample_society(
code text,
society text,
socde text)"""
)

s = ttk.Style()
s.configure("TNotebook.Tab", font=("times new roman", "14", "bold"))

tabControl = ttk.Notebook(root)

tab1 = Frame(tabControl)
tab2 = Frame(tabControl)

tabControl.add(tab1, text="entry with listbox")
tabControl.add(tab2, text="database entry")
tabControl.pack()


# 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()#forget can be used here for the re entry of the listbox 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(tab1, text="Start Typing...",
    font=("Helvetica", 14), fg="grey")

my_label.pack(pady=20)

# Create an entry box
my_entry = Entry(tab1, font=("Helvetica", 20))
my_entry.pack()

# Create a listbox
my_list = Listbox(tab1, width=50)


# Create a list of pizza toppings
conn = sqlite3.connect('society_sample.db')
conn.row_factory = lambda cursor, row: row[0]
c = conn.cursor()
c.execute("SELECT socde FROM sample_society")
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)

def add():
    # create a database or connect to one
    conn = sqlite3.connect('society_sample.db')
    # create cursor()
    c = conn.cursor()

    # insert into table
    c.execute(
        "INSERT INTO sample_society VALUES(:society,:code,:socde)",
        {
            'society': en1.get(),
            'code': en2.get(),
            'socde': en2.get()+"-"+en1.get()})

    # commit changes
    conn.commit()


    # close connection
    conn.close()
    en1.delete(0,END)
    en2.delete(0,END)

la1=Label(tab2,text="Society")
la1.grid(row=0,column=0)
en1=Entry(tab2)
en1.grid(row=0,column=1)

la2=Label(tab2,text="Code")
la2.grid(row=1,column=0)
en2=Entry(tab2)
en2.grid(row=1,column=1)


but25=Button(tab2,text="ADD",command=add)
but25.grid(row=2,column=0)

# commit changes
conn.commit()

# close connection
conn.close()


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 )