I have a TreeView in my Tkinter program where each entry correlates to an Object outside of the tree. Since Tkinter provides no direct way to associate objects with TreeView entries, I must bind functions to events in the tree in order to load the correct data. I use the "<<TreeviewSelect>>"
event to detect when an entry has been selected, then load the relevant data for the clicked entry. It works fine when I manually set the selection using selection_set
, but the event does not fire when an entry is clicked.
My code is:
def AddTask(self, task, parent="", select=False):
tag = str(uuid.uuid1())
treeItem = self.listTree.insert(parent, END, text = task.name, tags=tag)
self.listTree.tag_bind(tag, "<<TreeviewSelect>>", self.SelectTask(task))
if select:
self.listTree.selection_set(treeItem)
self.taskToItem[task] = treeItem
return treeItem
def SelectTask(self, task):
self.selectedTask = task
print("select")
This should result in a “select” being printed in the console every time an entry is clicked, but it only happens when an entry is first created using AddTask
with select
set to True
. This indicates that the <<TreeviewSelect>>
event does not fire on click.
listTree
is created like this:
self.listTree = ttk.Treeview(self.listFrame, show="tree", selectmode="browse")
self.listTree.pack(fill=BOTH, side=TOP, expand=True)
self.listTree.heading("#0", text="Name")
Cassowar y is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.