python - searching in treeview and highlight/select the row that contains the item that is searched -
i making simple gui patient's list patient's name , date of visiting, using tkinter , treeview, have entry user should type name of patient , idea if name of patient located in list, row (or rows) contain patient's name highlighted(selected). or other option can in listbox patients, display entries patient's name search for.
i have not used treeview before , not find data functions , examples, struggling selection/highlight part, ideas helpful @ point....
my code far is:
import tkinter tkinter import ttk class mainpage: def __init__(self,master): self.master = master self.frame = tkinter.frame(self.master) self.master.columnconfigure(0, weight=1) self.master.columnconfigure(1, weight=3) self.master.columnconfigure(2, weight=1) self.master.columnconfigure(3, weight=1) self.master.columnconfigure(4, weight=1) self.searchfield = tkinter.frame(self.master) self.searchfield.grid(row=1, column=0, columnspan=4) self.search_var = tkinter.stringvar() self.search_var.trace("w", lambda name, index, mode: self.selected) self.entry = tkinter.entry(self.searchfield, textvariable=self.search_var, width=45) self.entry.grid(row=0, column=0, padx=10, pady=3) self.searchbtn = tkinter.button(self.searchfield, text='search', command=self.selected) self.searchbtn.grid(row=0, column=1) self.treeframe = tkinter.listbox(self.searchfield, width=45, height=45) self.treeframe.grid(row=1, column=0, padx=10, pady=3) self.tree = ttk.treeview( self.treeframe, columns=('name', 'date')) self.tree.heading('#0', text='id') self.tree.heading('#1', text='name') self.tree.heading('#2', text='date') self.tree.column('#1', stretch=tkinter.yes) self.tree.column('#2', stretch=tkinter.yes) self.tree.column('#0', stretch=tkinter.yes) self.tree.grid(row=4, columnspan=4, sticky='nsew') self.treeview = self.tree self.i = 1 self.patient_list = [{"name": "jane", "date": "05.09.2017"}, {"name": "david", "date": "04.09.2017"}, {"name": "patrick", "date": "03.09.2017"}] p in self.patient_list: self.tree.insert('', 'end', text="id_"+str(self.i), values= (p["name"], p["date"])) self.i = self.i + 1 self.search_item = self.entry.get() p in self.patient_list: if p["name"] == self.search_item: self.selected(self.search_item) def selected(self): currentitem = self.tree.focus() print(self.tree.item(currentitem)['values']) root=tkinter.tk() d=mainpage(root) root.mainloop()
thanks in advance!
please see explained snippet below:
from tkinter import * tkinter import ttk class app: def __init__(self, root): self.root = root self.tree = ttk.treeview(self.root) #create tree self.sv = stringvar() #create stringvar entry widget self.sv.trace("w", self.command) #callback if stringvar updated self.entry = entry(self.root, textvariable=self.sv) #create entry self.names = ["jane", "janet", "james", "jamie"] #these test inputs tree self.ids = [] #creates list store ids of each entry in tree in range(len(self.names)): #creates entry in tree each element of list #then stores id of tree in self.ids list self.ids.append(self.tree.insert("", "end", text=self.names[i])) self.tree.pack() self.entry.pack() def command(self, *args): self.selections = [] #list of ids of matching tree entries in range(len(self.names)): #the below if check checks if value of entry matches first characters of each element #in names list length of value of entry widget if self.entry.get() != "" , self.entry.get() == self.names[i][:len(self.entry.get())]: self.selections.append(self.ids[i]) #if matches appends id selections list self.tree.selection_set(self.selections) #we select every id in list root = tk() app(root) root.mainloop()
so this, every time entry
widget updated, cycle through list of names , check if value of entry
widget matches value of element
in names
list
length of value of entry
widget (eg, if enter 5 character long string check against first 5 characters of element).
if match append id
of tree entry list
.
after names have been checked pass list
of matching id
s self.tree.selection_set()
highlights of matching tree entries.
Comments
Post a Comment