简体   繁体   English

如何在Python中使用Gtk3 GtkTreeRowReference

[英]How to use Gtk3 GtkTreeRowReference in Python

I'm trying to call Gtk3's GtkTreeRowReference() function without any success. 我试图调用Gtk3的GtkTreeRowReference()函数而没有任何成功。 I'm trying to delete multiple records from a ListStore using the associated TreeView's selection set to MULTIPLE mode. 我正在尝试使用关联的TreeView选择设置为MULTIPLE模式从ListStore删除多个记录。 I want to save a TreeRowReference for each ListStore item pointed to in the selection and use those to delete the ListStore items because those paths are supposed to get updated as ListStore items reached earlier in the selection get deleted. 我想为选择中指向的每个ListStore项保存TreeRowReference ,并使用它们删除ListStore项,因为这些路径应该更新,因为选择中较早到达的ListStore项被删除。 I've found many references to using TreeRowReferences in PyGtk 2 and the tutorial for PyGObject refers to their use but gives no actual example. 我发现许多在PyGtk 2中使用TreeRowReferences引用,而PyGObject的教程指的是它们的使用,但没有给出实际的例子。 I've tried many ways to invoke GtkTreeRowReference() without success. 我已经尝试了很多方法来调用GtkTreeRowReference()但没有成功。 For example: 例如:

hit_rows = []
for row in range(len(self.selection.get_selected_rows()):
    hit = self.selection.get_selected_rows()[row]
    hit_row = Gtk.TreeRowReference(liststore, hit)
    hit_rows.append(hit_row)

produces this fatal error message: "TypeError: function takes at most 0 arguments (2 given)" when my program hits the Gtk.TreeRowReference line. 产生这个致命的错误消息:当我的程序命中Gtk.TreeRowReference行时, "TypeError: function takes at most 0 arguments (2 given)" The rows in the selection already contain a reference to the ListStore , so tried again with only the selection row as an argument, but that just got the complaint that the function still insists on 0 arguments and I had tried to pass it 1 argument. 选择中的行已经包含对ListStore的引用,因此仅使用选择行作为参数再次尝试,但这只是抱怨该函数仍然坚持0参数并且我试图将它传递给1个参数。

I also tried things like this: 我也尝试过这样的事情:

hit_rows = []
for row in range(len(self.selection.get_selected_rows())):
    hit = self.selection.get_selected_rows()[row]
    hit_row = hit.GtkTreeRowReference()
    hit_rows.append(hit_row)

These efforts provoked Python to complain about an "AttributeError: 'ListStore' object has no attribute 'GtkTreeRowReference'." 这些努力激起了Python抱怨"AttributeError: 'ListStore' object has no attribute 'GtkTreeRowReference'." Varying the invocation to TreeRowReference , Gtk_TreeRowReference and several other variations produced the same error message. 改变对TreeRowReference的调用, Gtk_TreeRowReference和其他几个变体产生相同的错误消息。

Can anyone spare me a clue about how to use Gtk.TreeRowReference in PyGObject/Gtk3? 任何人都可以饶了我一个有关如何使用线索Gtk.TreeRowReference在PyGObject / Gtk3? As a relatively new and inexperienced Python programmer who's also new to Gtk, no doubt I've overlooked something breathtakingly obvious, but I'm stumped even after much internet searching. 作为一个相对较新且缺乏经验的Python程序员,他也是Gtk的新手,毫无疑问我忽略了一些令人惊叹的显而易见的事情,但即使经过互联网搜索,我也感到难过。

Instead of just calling Gtk.TreeRowReference now (PyGObject + GTK+ 3) you have to use Gtk.TreeRowReference.new . 现在不必只是调用Gtk.TreeRowReference (PyGObject + GTK + 3),而是必须使用Gtk.TreeRowReference.new For instance, you could use: 例如,您可以使用:

selection = treeview.get_selection()
model, paths = selection.get_selected_rows()
refs = []
for path in paths:
    refs.append(Gtk.TreeRowReference.new(model, path))

That is, treeview has been defined before. 也就是说,之前已经定义了treeview Then, you can double check it later with something like: 然后,您可以稍后再检查一下,例如:

for ref in refs:
    path = ref.get_path()
    iter = model.get_iter(path)
    value = model.get(iter, 0)[0]
    print '(%s, %s)' % (path, value)

In the last part, I assume you want to get the value of the first column in the model. 在最后一部分中,我假设您想获得模型中第一列的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM