Search code examples
pythontkintertreeviewttk

Get the order in which a treeviews selection occured?


The following code creates a treeview, fills it with dummy data, and prints out the selection when there is a change:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

myTree = ttk.Treeview(root)
myTree.grid()

#Dummy Data
for n, i in enumerate(['get','the','index','order']):
    myTree.insert(parent = "", index = "end", id = n, text = i)

def printSelected(*args):
    print(myTree.selection())

myTree.bind("<<TreeviewSelect>>", printSelected)

root.mainloop()

Currently it spits out the selection in a sorted order, How can I get the selection in a way that reflects how it was selected?

Example: The treeview was selected in the order of 4-2-3-1

Output:1-2-3-4

Desired Output:4-2-3-1


Solution

  • How can I get the selection in a way that reflects how it was selected?

    The treeview doesn't provide the ability to do that. You'll have to add your own bindings to track the order in which items are selected.