Click here to Skip to main content
15,946,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm trying to make an item search engine in a treeview, if I do it with a button, it works for me.

But I want to do the search without a button, just writing in the entry I select the closest thing.

I have done this, it does not give me any errors or anything, but it does not select any similar item or anything.

Python
self.entry_buscador = tk.Entry()
        self.entry_buscador.place(x=150, y=142, width=450, height=40)

self.entry_buscador.bind("<<KeyRelease>>", self.buscar)

def buscar(self, event):
        self.query = self.entry_buscador.get().lower()

        for item in self.treeview_almacen.get_children():
            values = self.treeview_almacen.item(item, "values")
            if self.query in values[0].lower():
                self.treeview_almacen.selection_set(item)
                self.treeview_almacen.focus(item)
                self.treeview_almacen.see(item)
            else:
                self.treeview_almacen.selection_remove(item)


What I have tried:

What do I have wrong? what can I do
Posted
Updated 11-Aug-23 15:12pm
v4
Comments
Richard MacCutchan 11-Aug-23 9:13am    
You need to use the debugger to trace through the event handler to see what is happening.

Your code is close but not enough information given. I have done some modifications to make sure the set-up is done correctly. I have also added dummy data as items to test.

You will need both the 'tkinter' and 'ttk' modules -
Python
import tkinter as tk
from tkinter import ttk

class ItemSearchApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Item Search Engine")

        #Create your Treeview...
        self.treeview_almacen = ttk.Treeview(root, columns=("item_name"))
        self.treeview_almacen.heading("item_name", text="Item Name")
        self.treeview_almacen.pack()

        #Create your Entry widget for search options...
        self.entry_buscador = ttk.Entry(root)
        self.entry_buscador.pack()
        
        #Bind the key release event to your search function...
        self.entry_buscador.bind("<KeyRelease>", self.buscar)

        #I have added sample data for the Treeview...
        self.populate_treeview()

    def populate_treeview(self):
        #Insert some sample items into the Treeview...
        items = ["Apple", "Banana", "Orange", "Pear", "Grapes", "Pineapple"]
        for item in items:
            self.treeview_almacen.insert("", "end", values=(item))

    def buscar(self, event):
        self.query = self.entry_buscador.get().lower()

        #Loop through Treeview items...
        for item in self.treeview_almacen.get_children():
            values = self.treeview_almacen.item(item, "values")
            if self.query in values[0].lower():
                self.treeview_almacen.selection_set(item)  #Select the matching item...
                self.treeview_almacen.focus(item)  #Focus on the matching item...
                self.treeview_almacen.see(item)  #Scroll to make the matching item visible...
            else:
                self.treeview_almacen.selection_remove(item)  #Deselect non-matching items...

#Create your main application window...
root = tk.Tk()
app = ItemSearchApp(root)
root.mainloop()


And voila, this should work for you.
 
Share this answer
 
Comments
Andre Oosthuizen 12-Aug-23 4:26am    
You're welcome Tomas. Please remove your comment as a solution and post it here under the comments, thank you.
Thank you very much, yes it works correctly.

solution:


Python
self.entry_finder.bind("<KeyRelease>", self.find)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900