简体   繁体   中英

How to use Gtk3 GtkTreeRowReference in Python

I'm trying to call Gtk3's GtkTreeRowReference() function without any success. I'm trying to delete multiple records from a ListStore using the associated TreeView's selection set to MULTIPLE mode. 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. 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. I've tried many ways to invoke GtkTreeRowReference() without success. 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. 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.

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'." Varying the invocation to TreeRowReference , Gtk_TreeRowReference and several other variations produced the same error message.

Can anyone spare me a clue about how to use Gtk.TreeRowReference in 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.

Instead of just calling Gtk.TreeRowReference now (PyGObject + GTK+ 3) you have to use 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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