I have a treeview in tkinter using python 3. The problem is that when I bind a right-click to get the row ID of the right-clicked row, I end up getting the actual row ID of the previous event. For example, I might right-click "Project 1", this will return me "" and then I right-click "Project 2" and it returns "Project 1" as rowID.
def initTreeView(self):
self.treeView = ttk.Treeview(self.treeSectionFrame)
self.treeView.heading('#0', text='Projects')
self.treeView.grid(row=0, column=0, sticky=("N", "S", "E", "W"))
self.treeView.bind('<3>', self.rightClickMenu)
def rightClickMenu(self, event):
def hello():
print("hello!")
# create a popup menu
print(event.x, event.y)
rowID = self.treeView.identify('item', event.x, event.y)
if rowID:
menu = Menu(self.root, tearoff=0)
menu.add_command(label="Undo", command=hello)
menu.add_command(label="Redo", command=hello)
menu.post(event.x_root, event.y_root)
self.treeView.selection_set(rowID)
self.treeView.focus_set()
self.treeView.focus(rowID)
print(rowID)
else:
pass
Thanks,
[EDIT]
I've found a dirty hack which consists of making the tags of each item the same as its id so you can then fetch the actual rowID. This can also be done using the value option.
self.treeView.insert("", "end", "id-1, tags="id-1", text="Project 1")
...
rowID = self.treeView.identify('item', event.x, event.y)
rowID = self.treeView.item(rowID)["tags"] # gives you actual ID