pythontkintersearch-box

Partial search in python tkinter doesn't work


I've got a problem with a part of my code. It's about partial searching in treeview. I found here on stack overflow partial search method and tried to use with my code. It does't work - It doesn't give any results. Code here below:

from tkinter import *
from tkinter import ttk
root = Tk()
sv = StringVar()
ids = []
names = []
def add():
    names = tree.insert("",END,values=(e0.get(),e1.get(),e2.get(),e3.get()))

for i in range(len(names)):
    ids.append(tree.insert("", "end", text=names[i]))
 
def command(*args):
    selections = []
    for i in range(len(names)):
        if entry.get() != "" and entry.get() == names[i][:len(entry.get())]:
            selections.append(ids[i])
        tree.selection_set(selections)
sv.trace("w", command)
entry = Entry(root, textvariable=sv,width=13)
entry.grid(row=2,column=1,rowspan=3,sticky=W)
e0 = Entry(root,width=15)
e0.grid(row=0,column=1,rowspan=1,sticky=W)
e1 = Entry(root,width=15)
e1.grid(row=0,column=1,rowspan=2,sticky=W)
e2 = Entry(root,width=15)
e2.grid(row=0,column=1,rowspan=3,sticky=W)
e3 = Entry(root,width=15)
e3.grid(row=0,column=1,rowspan=4,sticky=W)
 
btn1 = Button(root,text="add",width=5,command=add)
btn1.grid(row =0,column=0,rowspan=5)
lb1 = Label(root,text="serial num:")
lb1.grid(row =0,column=0,rowspan=1)
lb2 = Label(root,text="medicine\nname ")
lb2.grid(row =0,column=0,rowspan=2)
lb3 = Label(root,text="quatity")
lb3.grid(row =0,column=0,rowspan=3)
lb4 = Label(root,text="expiry Date")
lb4.grid(row =0,column=0,rowspan=4)
lb4 = Label(root,text="search box")
lb4.grid(row =1,column=0,rowspan=6)
#treeview
tree = ttk.Treeview(root,height=25)
tree["columns"]=("one","two","three","four")
tree.column("one",width=120)
tree.column("two",width=160)
tree.column("three",width=130)
tree.column("four",width=160)
tree.heading("one", text="Numer seryjny leku")
tree.heading("two", text="Nazwa Leku")
tree.heading("three", text="Ampułki/Tabletki")
tree.heading("four",text="Data ważności")
tree["show"]="headings"
tree.grid(row=0,column=2,rowspan=6,pady=20)
root.geometry("840x580")
root.mainloop()

Solution

  • The variable names is not defined. You should put a similar line to the following one in the beginning of your code:

    names = []
    

    On the other hand, you have to declare the command function as follows to make it work, since the trace callback expects at least three arguments:

    def command(*args):
    

    By the way, if you do not want to loose the data in variable names, I would transform your code in a class-oriented way, such as the following one:

    from tkinter import *
    from tkinter import ttk
    root = Tk()
    sv = StringVar()
    ids = []
    
    
    class Tree():
        def __init__(self, root):
            self.names = []
            sv.trace("w", self.command)
            self.entry = Entry(root, textvariable=sv, width=13)
            self.entry.grid(row=2,column=1,rowspan=3,sticky=W)
            self.e0 = Entry(root,width=15)
            self.e0.grid(row=0,column=1,rowspan=1,sticky=W)
            self.e1 = Entry(root,width=15)
            self.e1.grid(row=0,column=1,rowspan=2,sticky=W)
            self.e2 = Entry(root,width=15)
            self.e2.grid(row=0,column=1,rowspan=3,sticky=W)
            self.e3 = Entry(root,width=15)
            self.e3.grid(row=0,column=1,rowspan=4,sticky=W)
            
            self.btn1 = Button(root,text="add",width=5,command=self.add)
            self.btn1.grid(row =0,column=0,rowspan=5)
            self.lb1 = Label(root,text="serial num:")
            self.lb1.grid(row =0,column=0,rowspan=1)
            self.lb2 = Label(root,text="medicine\nname ")
            self.lb2.grid(row =0,column=0,rowspan=2)
            self.lb3 = Label(root,text="quatity")
            self.lb3.grid(row =0,column=0,rowspan=3)
            self.lb4 = Label(root,text="expiry Date")
            self.lb4.grid(row =0,column=0,rowspan=4)
            self.lb4 = Label(root,text="search box")
            self.lb4.grid(row =1,column=0,rowspan=6)
            #treeview
            self.tree = ttk.Treeview(root,height=25)
            self.tree["columns"]=("one","two","three","four")
            self.tree.column("one",width=120)
            self.tree.column("two",width=160)
            self.tree.column("three",width=130)
            self.tree.column("four",width=160)
            self.tree.heading("one", text="Numer seryjny leku")
            self.tree.heading("two", text="Nazwa Leku")
            self.tree.heading("three", text="Ampułki/Tabletki")
            self.tree.heading("four",text="Data ważności")
            self.tree["show"]="headings"
            self.tree.grid(row=0,column=2,rowspan=6,pady=20)
        
        def add(self):
            self.names = self.tree.insert("",END,values=(self.e0.get(),self.e1.get(),self.e2.get(),self.e3.get()))
    
            for i in range(len(self.names)):
                ids.append(self.tree.insert("", "end", text=self.names[i]))
        
        def command(self, *args):
            selections = []
            for i in range(len(self.names)):
                if self.entry.get() != "" and self.entry.get() == self.names[i][:len(self.entry.get())]:
                    selections.append(ids[i])
                self.tree.selection_set(selections)
    
    tree = Tree(root)
    root.geometry("840x580")
    root.mainloop()