2015-09-20 3 views
1

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

Я нашел 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) 

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

ответ

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) 
Смежные вопросы