0

I want to get the index of a row in a Tkinter treeview without clicking on the row. I have a three-column treeview that displays correctly. In the first column, I have values, each unique.

For example, I would like to enter one of the values ​​in the first column (12, for example) and use a function to display the ID of the corresponding row in the treeview.

Here is my code and the result it gives me.

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Gestion des données avec Treeview")
tree = ttk.Treeview(root,columns=("nom", "prenom", "age"))
tree.heading("nom",text="Nom")
tree.heading("prenom",text="Prénom")
tree.heading("age",text="Âge")
tree.column("nom",width=100)
tree.column("prenom",width=100)
tree.column("age",width=50,anchor="center")
# Insertion de données dans le Treeview
tree.insert("", "end",text="10",values=("Dupont", "Jean", 25))
tree.insert("", "end",text="12",values=("Martin", "Claire", 30))
tree.insert("", "end",text="25",values=("Durand", "Pierre", 22))
# Placement du Treeview dans la fenêtre
tree.pack(fill=tk.BOTH,expand=True)
root.mainloop()
1
  • You can go through all the rows and check whether the input value is the same as that of the first column. Commented Apr 25 at 6:06

1 Answer 1

1

Here's a minimal "search" function that should do what you want. If the value entered in the search_entry matches the 'text' value of any item in tree, then that item will be selected.

Since you want the index of search result, I've added an IntVar to store the match_index. You could also change this to a StringVar and store the iid value instead (and remove the need for enumerate).

import tkinter as tk
from tkinter import ttk


def search(*_args) -> None:  # args are passed in by the trace, but unused
    query = search_var.get()
    for index, iid in enumerate(tree.get_children()):
        if query == tree.item(iid, 'text'):
            tree.selection_set(iid)  # highlight the match
            match_index.set(index)
            break  # then stop looking for matches
    else:
        tree.selection_set('')  # no match, clear any existing selections


root = tk.Tk()
root.title("Gestion des données avec Treeview")

search_var = tk.StringVar(root, '')
search_entry = ttk.Entry(root, textvariable=search_var)
search_entry.pack()
# use a variable trace to call 'search' whenever 'search_var' changes
search_var.trace_add('write', search)  
match_index = tk.IntVar(root, None)  # stores the latest search result's index

tree = ttk.Treeview(root,columns=("nom", "prenom", "age"))
tree.heading("nom",text="Nom")
tree.heading("prenom",text="Prénom")
tree.heading("age",text="Âge")
tree.column("nom",width=100)
tree.column("prenom",width=100)
tree.column("age",width=50,anchor="center")
# Insertion de données dans le Treeview
tree.insert("", "end",text="10",values=("Dupont", "Jean", 25))
tree.insert("", "end",text="12",values=("Martin", "Claire", 30))
tree.insert("", "end",text="25",values=("Durand", "Pierre", 22))
# Placement du Treeview dans la fenêtre
tree.pack(fill=tk.BOTH,expand=True)
root.mainloop()
Sign up to request clarification or add additional context in comments.

4 Comments

Should the else be for the for loop inside search()?
@acw1668 ah yep! Good catch thanks
Also tree.item(iid, 'text') is better than tree.item(iid)['text'].
@acw1668 totally forgot you could do that - thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.