Skip to main content

entry box with the automatic listbox

 Here in this coding we are going to show the entry connected with the listbox in python.

so here is the following coding as respective to the needs.





=========================================================================

import tkinter as tk


def on_change(*args):
    # print(args)

    value = var_text.get()
    value = value.strip().lower()

    # get data from test_list
    if value == '':
        data = test_list
    else:
        data = []
        for item in test_list:
            if value in item.lower():
                data.append(item)

                # update data in listbox
    listbox_update(data)


def listbox_update(data):
    # delete previous data
    listbox.delete(0, 'end')

    # sorting data
    data = sorted(data, key=str.lower)

    # put new data
    for item in data:
        listbox.insert('end', item)


def on_select(event):
    # display element selected on list
    print('(event) previous:', event.widget.get('active'))
    print('(event)  current:', event.widget.get(event.widget.curselection()))
    print('---')


# --- main ---

test_list = ('apple', 'banana', 'Cranberry', 'dogwood', 'alpha', 'Acorn', 'Anise', 'Strawberry')

root = tk.Tk()

var_text = tk.StringVar()
var_text.trace('w', on_change)

entry = tk.Entry(root, textvariable=var_text)
entry.pack()

listbox = tk.Listbox(root)
listbox.pack()
# listbox.bind('<Double-Button-1>', on_select)
listbox.bind('<<ListboxSelect>>', on_select)
listbox_update(test_list)

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 )