Python gtk3 treeview right click doesn't select the right one

I have the following code, and when I select the line directly with the right button, the previous selected item gives it, not the current one that I just clicked.

What am I missing?

#!/usr/bin/env python3

from gi.repository import Gtk, GLib, Gio

class Test(Gtk.Window):
  def __init__(self):
    Gtk.Window.__init__(self)
    store = Gtk.ListStore(str)
    self.tree = Gtk.TreeView(store)
    for i in range(0,10):
        store.append(["test " + str(i)])
    self.connect("delete-event", Gtk.main_quit)
    self.tree.connect("button_press_event", self.mouse_click)

    renderer = Gtk.CellRendererText()
    column = Gtk.TreeViewColumn("Title", renderer, text=0)
    self.tree.append_column(column)
    self.add(self.tree)

def mouse_click(self, tv, event):
    if event.button == 3:
        selection = self.tree.get_selection()
        (model, iter) = selection.get_selected()
        print(model[iter][0])

win = Test()
win.show_all()
Gtk.main()
+4
source share
2 answers

The trick is to use treeview.get_path_at_pos(x, y)using coordinates sent using the button click event. When you have a path, you have everything you need to move on.

Here is a complete working example, pay attention to additional lines in mouse_click()

#!/usr/bin/env python3

from gi.repository import Gtk, GLib, Gio

class Test(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        store = Gtk.ListStore(str)
        self.tree = Gtk.TreeView(store)
        for i in range(0,10):
            store.append(["test " + str(i)])
        self.connect("delete-event", Gtk.main_quit)
        self.tree.connect("button_press_event", self.mouse_click)

        renderer = Gtk.CellRendererText()
        column = Gtk.TreeViewColumn("Title", renderer, text=0)
        self.tree.append_column(column)
        self.add(self.tree)

    def mouse_click(self, tv, event):
        if event.button == 3:
            # Begin added code
            pthinfo = self.tree.get_path_at_pos(event.x, event.y)
            if pthinfo != None:
                path,col,cellx,celly = pthinfo
                self.tree.grab_focus()
                self.tree.set_cursor(path,col,0)
            # End added code

            selection = self.tree.get_selection()
            (model, iter) = selection.get_selected()
            print(model[iter][0])

win = Test()
win.show_all()
Gtk.main()
+3
source

vala, @Kristof, , - .

//add mouse click listener
            device_table_treeview.button_press_event.connect ((widget, event) => {
                //capture which mouse button
                uint mouseButtonClicked;
                event.get_button(out mouseButtonClicked);
                //handle right button click for context menu
                if (event.get_event_type ()  == Gdk.EventType.BUTTON_PRESS  &&  mouseButtonClicked == 3){
                    debug("Show Context Menu");
                    //determine the position on which the right click has happened          
                    TreePath path; TreeViewColumn column; int cell_x; int cell_y;
                    device_table_treeview.get_path_at_pos ((int)event.x, (int)event.y, out path, out column, out cell_x, out cell_y);
                    device_table_treeview.grab_focus();
                    device_table_treeview.set_cursor(path,column,false);
                    //get details of the selected row and display the context menu
                    TreeSelection aTreeSelection = device_table_treeview.get_selection ();
                    if(aTreeSelection.count_selected_rows() == 1){
                        TreeModel model;
                        TreeIter iter;
                        aTreeSelection.get_selected (out model, out iter);
                        deviceContextMenu(model, iter);
                    }
                }
                return false;
            }); 

, GitHub: https://github.com/babluboy/nutty/blob/master/src/devices.vala

0

All Articles