Python gtk3 treeview右键单击未选择正确的选择

时间:2015-01-22 20:01:52

标签: python gtk gtk3

我有以下代码,当我用右键直接选择一行时,它输出上一个选中的项目,而不是刚刚点击的当前项目。

我缺少什么?

#!/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()

3 个答案:

答案 0 :(得分:3)

诀窍是使用treeview.get_path_at_pos(x, y),使用按钮按下事件发送的坐标。一旦你有了一条道路,你就拥有了进一步发展所需的一切。

这是一个完整的工作示例,请注意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()

答案 1 :(得分:0)

这是一个来自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

答案 2 :(得分:0)

使用“ button_release_event”而不是“ button_press_event”来确保在执行任何进一步操作之前已选择了单击的行。

self.tree.connect("button_release_event", self.mouse_click)