bind key click to selected item from a tree with python tkinter
First of all, bind the right click on the tree, not on the frame, i.e. replace self.frame.bind("<Button-3>", self.click)
by self.tree.bind("<Button-3>", self.click)
.
Then, you need to know on which item in the treeview the user has clicked. For that you can use self.tree.identify_row(event.y)
which returns the id of the item at position event.y
.
And, if you need to, you can retrieve the information on the patient stored in the treeview with self.tree.item(<item>, 'values')
.
So click
becomes:
def click(self, event): item = self.tree.identify_row(event.y) if item: # right click on a patient self.master.withdraw() self.toplevel = tkinter.Toplevel(self.master) self.toplevel.geometry("480x480") info = self.tree.item(item, 'values') print(info) app = PatientPage(self.toplevel)