Python GTK 3-TreeView-允许鼠标选择行以复制内容



我是Python GTK的新手,我正在开发一个显示几个数据的简单TreeView:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
# list of tuples for each variable, containing the environment variable name, its path, and the security category
variable_list = [
("$ENV1", "/usr/share/background/image.png", "Image"),
("$ENV2", "/home/user/script.py", "Program"),
("$ENV3", "/usr/share/icons/icon.png", "Image")
]

class TreeViewFilterWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Environment Variable View")
self.set_border_width(10)
# Setting up the self.grid in which the elements are to be positioned
self.grid = Gtk.Grid()
self.grid.set_column_homogeneous(True)
self.grid.set_row_homogeneous(True)
self.add(self.grid)
# Creating the ListStore model
self.variable_liststore = Gtk.ListStore(str, str, str)
for variable_ref in variable_list:
self.variable_liststore.append(list(variable_ref))
self.current_filter_category = None
# Creating the filter, feeding it with the liststore model
self.category_filter = self.variable_liststore.filter_new()
# setting the filter function, note that we're not using the
self.category_filter.set_visible_func(self.category_filter_func)
# creating the treeview, making it use the filter as a model, and adding the columns
self.treeview = Gtk.TreeView(model=self.category_filter)
for i, column_title in enumerate(
["Variable", "Path", "Category"]
):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(column_title, renderer, text=i)
self.treeview.append_column(column)
# creating buttons to filter by Category, and setting up their events
self.buttons = list()
for var_category in ["Image", "Program"]:
button = Gtk.Button(label=var_category)
self.buttons.append(button)
button.connect("clicked", self.on_selection_button_clicked)
# setting up the layout, putting the treeview in a scrollwindow, and the buttons in a row
self.scrollable_treelist = Gtk.ScrolledWindow()
self.scrollable_treelist.set_vexpand(True)
self.grid.attach(self.scrollable_treelist, 0, 0, 5, 10)
self.grid.attach_next_to(
self.buttons[0], self.scrollable_treelist, Gtk.PositionType.BOTTOM, 1, 1
)
for i, button in enumerate(self.buttons[1:]):
self.grid.attach_next_to(
button, self.buttons[i], Gtk.PositionType.RIGHT, 1, 1
)
self.scrollable_treelist.add(self.treeview)
self.show_all()
def category_filter_func(self, model, iter, data):
"""Tests if the Category in the row is the one in the filter"""
if (
self.current_filter_category is None
or self.current_filter_category == "None"
):
return True
else:
return model[iter][2] == self.current_filter_category
def on_selection_button_clicked(self, widget):
"""Called on any of the button clicks"""
# we set the current category filter to the button's label
self.current_filter_category = widget.get_label()
print("%s category selected!" % self.current_filter_category)
# we update the filter, which updates in turn the view
self.category_filter.refilter()

win = TreeViewFilterWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

运行脚本时,无法选择每一行的内容。有没有办法选择这些行的内容,以便我可以右键单击并复制内容?

如果我想用鼠标光标突出显示文本,然后直接将突出显示的内容复制到剪贴板上;内容已被复制";,我怎样才能达到这些目的?

如果选择是可能的,这必须通过在Gtk.CellRendererText中设置一个属性来完成,但除了editable:之外,我没有看到任何属性可以实现这一点

renderer.set_property("editable", True)

使单元格可编辑当然允许用户从中复制,但这意味着内容可以更改。(如果你没有连接到edited信号,文本在输入后会恢复。这可能会让人觉得非常违反直觉。(

如果你有时间的话,你可以写你自己的CellRenderer(我没有这个经验(

另一种可能性是连接到Gtk.TreeViewbutton-released-event,如果用户右键单击,则将文本复制到剪贴板。使用Gtk.TreeView.get_path_at_pos获取光标下的单元格。

def listclick(self, listview, event: Gdk.EventButton, *_unused) -> bool:
"""Handler for clicks on the list."""
if (event.type == Gdk.EventType.BUTTON_RELEASE and event.button == 3):
tup = listview.get_path_at_pos(event.x, event.y)
if tup is not None:
path, col, *unused = tup
# Copy to Clipboard, depending on Path and Column
# ...
return True
return False

相关内容

  • 没有找到相关文章

最新更新