Кнопка для удаления строки в GTK 3

Я пытаюсь сделать свое первое настольное приложение на Python и GTK3, но быстро сталкиваюсь с проблемой. Я хочу отобразить TreeView с URL-адресом столбцов, заголовком и значком удаления, но у меня проблема с отображением и значком, который можно щелкнуть, и который удаляет строку.

Я нашел этот вопрос, но решение мне не помогло .

Есть ли способ сделать то, что я хочу? Или я неправильно спроектировал?

Код:

    # 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)

Спасибо за помощь


person pawel.ad    schedule 20.09.2015    source источник


Ответы (1)


Хитрость заключается в том, чтобы использовать активацию строки в 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)
person B8vrede    schedule 04.02.2016