Header Ads

Header ADS

Listbox that Gives suggestion


Tkinter Listbox that Gives suggestion.

Hey Guys , welcome to this Tkinter Blog .
In this Blog we will create a Tkinter Listbox which gives suggestion according to the values on a entry box.
It is like a autocomplete entry box which suggestion are comes into listbox.

Let me show you how it works.



As you can see in the video when we write some words in the entry box the listbox gives suggestion according to the entry box values.


So lets code it .

       			 
# Tkinter - Dynamic Coding
# listbox that gives suggestion

# import libraries .....
from tkinter import *      
# Function for checking the 
# key pressed and updating 
# the listbox 
def checkkey(event): 
       
    value = event.widget.get() 
    # value = val.get() # or you can use this ... 
    print(value) 
      
    # get data from l 
    if value == '': 
        data = l 
    else: 
        data = [] 
        for item in l: 
            if value.lower() in item.lower(): 
                data.append(item)                 
   
    # update data in listbox 
    update(data) 
   
   
def update(data): 
      
    # clear previous data 
    lb.delete(0, 'end') 
    lb.configure(height=len(data))
   
    # put new data 
    for item in data: 
        lb.insert('end', item) 
  
def go(event):
    cs = lb.get(ANCHOR)
    val.set(cs)
    

# Driver code 
l = ('C','C++','Java', 
     'Python','Perl', 
     'PHP','ASP','JS' ,'HTML','Go') 
  
root = Tk() 
root.title("Dynamic Coding")
root.geometry('300x200')

val = StringVar()  
#creating text box  
e = Entry(root,textvariable=val) 
e.pack(pady=10) 
e.bind('', checkkey) 
  
#creating list box 
lb = Listbox(root,bd=3,relief='groove') 
lb.bind('<<ListboxSelect>>', go)
lb.pack(pady=10) 
update(l) 
   
root.mainloop() 

 

The above code will create a suggestion list box for your entry box ..
.
you can check out our Tkinter tutorial series .
and leave a comment whether it is useful for you or not .

.



Recommended Posts:





Thank you ..
If have Any doubts and Errors , comment section is open for you and we will try to solve your doubts as soon as possible.

No comments

Powered by Blogger.