GTK 3 中的行删除按钮

Button for row deletion in GTK 3

我试图在 Python 和 GTK3 中开发我的第一个桌面应用程序,但我很快 运行 遇到了问题。 我想显示一个包含列 URL、标题和删除图标的 TreeView,但我在显示和可以单击的图标时遇到问题,这会删除该行。

我发现了 that 个问题,但解决方案对我不起作用。

有什么方法可以做我想做的事吗?还是我设计错了?

代码:

    # List
    self.store = Gtk.ListStore(str, str, str)
    self.store.append(['https://www.youtube.com/watch?v=dQw4w9WgXcQ',  # URL
                       'Rick Astley - Never Gonna Give You Up',  # Title
                       'edit-delete'])  # Action icon
    tree = Gtk.TreeView(self.store)
    tree.set_size_request(600, 400)

    # Editable URL
    url = Gtk.CellRendererText()
    url.set_property("editable", True)
    url.connect("edited", self.text_edited)
    column_url = Gtk.TreeViewColumn("YouTube URL", url, text=0)
    column_url.set_min_width(300)
    tree.append_column(column_url)

    # Title
    title = Gtk.CellRendererText()
    column_title = Gtk.TreeViewColumn("Title", title, text=1)
    tree.append_column(column_title)

    # Action icon
    action_icon = Gtk.CellRendererPixbuf()
    # action_icon.connect("clicked", self.action_icon_clicked)
    column_action_icon = Gtk.TreeViewColumn("", action_icon, icon_name=2)
    tree.append_column(column_action_icon)

感谢帮助

诀窍是利用 Treeview 中的行激活来捕获按钮是否被单击。由于 row_activated 告诉您点击了哪一列和哪一行,这样我们就可以删除点击的行。

Treeview 的默认行为是双击激活,但可以使用 tree.set_activate_on_single_click(True) 将其更改为单击。现在连接到信号的侦听器 tree.connect("row_activated", self.action_icon_clicked) 我们可以使用下面的函数删除单击的行。

def action_icon_clicked(self, treeview, path, column):
    # If the column clicked is the action column remove the clicked row
    if column is self.column_action_icon:

        # Get the iter that points to the clicked row
        iter = self.store.get_iter(path)

        # Remove it from the ListStore
        self.store.remove(iter)

所以完整的代码会变成:

    # List
    self.store = Gtk.ListStore(str, str, str)
    self.store.append(['https://www.youtube.com/watch?v=dQw4w9WgXcQ',  # URL
                       'Rick Astley - Never Gonna Give You Up',  # Title
                       'edit-delete'])  # Action icon
    tree = Gtk.TreeView(self.store)
    tree.set_size_request(600, 400)

    # Editable URL
    url = Gtk.CellRendererText()
    url.set_property("editable", True)
    column_url = Gtk.TreeViewColumn("YouTube URL", url, text=0)
    column_url.set_min_width(300)
    tree.append_column(column_url)

    # Title
    title = Gtk.CellRendererText()
    column_title = Gtk.TreeViewColumn("Title", title, text=1)
    tree.append_column(column_title)

    # Action icon
    action_icon = Gtk.CellRendererPixbuf()
    self.column_action_icon = Gtk.TreeViewColumn("", action_icon, icon_name=2)
    tree.append_column(self.column_action_icon)

    # Make a click activate a row such that we get the row_activated signal when it is clicked
    tree.set_activate_on_single_click(True)

    # Connect a listener to the row_activated signal to check whether the correct column was clicked
    tree.connect("row_activated", self.action_icon_clicked)

def action_icon_clicked(self, treeview, path, column):
    # If the column clicked is the action column remove the clicked row
    if column is self.column_action_icon:

        # Get the iter that points to the clicked row
        iter = self.store.get_iter(path)

        # Remove it from the ListStore
        self.store.remove(iter)