[Python] Selection item in treeview tkinter python

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    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()

    Continue reading...

Compartilhe esta Página