使用PyGObject Introspection和Gtk+ 3创建通用树模型?

3
我想编写一个基于Python3的Gtk+3-TreeModel,但我遇到了这个错误:
AttributeError: 'gi.repository.Gtk' object has no attribute 'GenericTreeModel'
“GenericTreeModel”已经更名了吗?
谢谢!
3个回答

4

PyGObject最近通过pygtkcompat获得了GenericTreeModel支持。

这是在3.7.90中新增的,修复于3.7.91

因此,现在您应该能够使用兼容模块迁移GenericTreeModels,至少作为一个开始。


3

我已经应对这个问题有一段时间了,以下是我的结果:

  • As Havok already said: the GenericTreeModel does not exist anymore and one has to use the normal gtk.TreeModel interface and override the appropriate methods (naming them do_…):

    class TreeModel(GObject.GObject, gtk.TreeModel):
       def do_get_iter(self, iter, path):
           …
           iter.user_data = whatever()
           return True
    
  • Inheriting from ListStore or TreeStore does not work when using custom iters etc.

  • The repository with call information is broken (see launchpad:gtk3#1024492) so that the do_get_iter method is called without the iter argument, so you cannot set custom data on it. To fix it change the direction for the iter argument in /usr/share/gir-1.0/Gtk-3.0.gir from "out" to "in" and run:

    g-ir-compiler --output=/usr/lib/girepository-1.0/Gtk-3.0.typelib /usr/share/gir-1.0/Gtk-3.0.gir
    

1

我在PyGObject和Gtk中都找不到GenericTreeModel的任何参考资料,但我认为你要找的只是TreeModel:

http://developer.gnome.org/gtk3/stable/GtkTreeModel.html

TreeModel是一个接口,由ListStore、TreeModelFilter、TreeModelSort和TreeStore实现。

>>> from gi.repository import Gtk
>>> dir(Gtk.TreeModel)
['__bool__', '__class__', '__delattr__', '__delitem__', '__dict__', '__doc__',
 '__format__', '__gdoc__', '__getattribute__', '__getitem__', '__gtype__', '__hash__', 
 '__info__', '__init__', '__iter__', '__len__', '__module__', '__new__', '__nonzero__',
 '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', 
 '__str__', '__subclasshook__', '__weakref__', '_convert_row', '_convert_value', 
 '_getiter', 'filter_new', 'foreach', 'get', 'get_column_type', 'get_flags', 'get_iter',
 'get_iter_first', 'get_iter_from_string', 'get_n_columns', 'get_path', 
 'get_string_from_iter', 'get_value', 'iter_children', 'iter_has_child', 
 'iter_n_children', 'iter_next', 'iter_nth_child', 'iter_parent', 'iter_previous', 
 'ref_node', 'row_changed', 'row_deleted', 'row_has_child_toggled', 'row_inserted', 
 'set_row', 'sort_new_with_model', 'unref_node']

编辑:

在旧的PyGtk API中找到了你要找的内容,不幸的是,这是一个仅限于PyGtk的创作。使用introspection只能获得Gtk直接提供的内容,因此您需要直接处理TreeModel。

希望有所帮助。


@JanHolthuis: 你能报告一下你的进展吗? 我试着将基于GenericTreeModel的应用程序移植到pygobject,但还没有成功。 - rumpel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接