【问题标题】:Button for row deletion in GTK 3GTK 3 中的行删除按钮
【发布时间】:2015-09-20 11:20:32
【问题描述】:

我尝试用 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)

感谢您的帮助

【问题讨论】:

    标签: python treeview gtk3 pygobject


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      • 1970-01-01
      相关资源
      最近更新 更多