Python tkinter treeview right-click (Button-3) to select an item in treeview

Using Python 3.2 and tkinter. How do you do Button-3 (right-click), select the item in the Treeview widgets that the mouse cursor is over? Basically, I want the Button-3 event to select an element in the same way as the current single left click.

+5
source share
1 answer
You have half answered your question. Just coded and tested my code, so I did not find any point in posting my fragment of the solution.
def init(self):
    """initialise dialog"""
    # Button-3 is right click on windows
    self.tree.bind("<Button-3>", self.popup)

def popup(self, event):
    """action in event of button 3 on tree view"""
    # select row under mouse
    iid = self.tree.identify_row(event.y)
    if iid:
        # mouse pointer over item
        self.tree.selection_set(iid)
        self.contextMenu.post(event.x_root, event.y_root)            
    else:
        # mouse pointer not over item
        # occurs when items do not fill frame
        # no action required
        pass
+6
source

All Articles