Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python tkinter treeview right click (Button-3) event to select item in treeview

Using Python 3.2 and tkinter. How do you make the Button-3 (right click) select the item in a Treeview widget that the mouse cursor is hovering over? I basically want the Button-3 event to select the item in the same way as the current single left click does.

like image 640
Twist Avatar asked Aug 20 '12 19:08

Twist


1 Answers

You half answered your own question. Just coded and tested my code, so thought no harm in posting my solution snippet here.

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
like image 102
joec Avatar answered Sep 25 '22 02:09

joec