gpt4 book ai didi

python - GTK 3 中的行删除按钮

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:04 25 4
gpt4 key购买 nike

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

关于python - GTK 3 中的行删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32678785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com