Я пытаюсь реализовать поиск в таблице ttk.treeview. Очевидно, уже есть некоторые ответы, и я использую один из них (см. код ниже). Однако я хочу расширить вопрос.
Учитывая приведенный ниже код, как сделать так, чтобы в дополнение к выделению нужного элемента представление прокручивалось до этого элемента?
Другими словами, если у нас будет больше элементов в списке, которые помещаются в представление, а тот, который мы ищем, находится внизу, как вывести его, скажем, в середину (или лучше вверх?) представления во время ввода символов в поле поиска?
Кроме того, поскольку можно выделить несколько элементов одновременно, я думаю, нам следует прокрутить до первого элемента.
from tkinter import *
from 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 for entry widget
self.sv.trace("w", self.command) #callback if stringvar is updated
self.entry = Entry(self.root, textvariable=self.sv) #create entry
self.names = [
"tawymu","uzzkhv","mimfms","qpugux","mswzaz","alexsa","khfpke",
"fsphkn","indzsl","rmvuag","gvrmxd","vfshxx","kwpuyz","pyfmar",
] # these are just test inputs for the tree
self.ids = [] #creates a list to store the ids of each entry in the tree
for i in range(len(self.names)):
#creates an entry in the tree for each element of the list
#then stores the id of the tree in the 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
for i in range(len(self.names)):
#the below if check checks if the value of the entry matches the first characters of each element
#in the names list up to the length of the value of the entry widget
if self.entry.get() != "" and self.entry.get() == self.names[i][:len(self.entry.get())]:
self.selections.append(self.ids[i]) #if it matches it appends the id to the selections list
self.tree.selection_set(self.selections) #we then select every id in the list
root = Tk()
App(root)
root.mainloop()






Есть see метод для treeview.
def command(self, *args):
self.selections = []
for i in range(len(self.names)):
if self.entry.get() != "" and self.entry.get() == self.names[i][:len(self.entry.get())]:
self.selections.append(self.ids[i])
self.tree.selection_set(self.selections)
try:
self.tree.see(self.selections[0]) #you can iterate through the list for multiple selection
except IndexError:
pass
Я нашел хак, чтобы вывести строку интереса в верхнюю часть окна. Сначала скользите вниз, а затем обратно вверх.
tree.see(items[-1])
tree.see(items[my_target_item_n])
Одним из решений является вычисление позиции первой выделенной строки относительно
scrollregionи использованиеself.tree.yview_moveto(<relative pos>).