GTK+ 3.0: How to use a Gtk.TreeStore with custom model items?

♀尐吖头ヾ 提交于 2019-12-03 13:34:22

问题


I'm trying to develop a GTK application in Python and I'm really stuck with the correct usage of a gtk.TreeStore. My main problem: I've already parsed some JSON and I have my own data structure which ist basically a Python list and two kinds of objects: One represents a collection of items (collections can't be nested) and one for representing items (which might appear in the list as well as in a collection).

I'm already familiar with the basic usage of a TreeStore and managed to get items correctly rendered on screen. I don't know how to deal with the fact that a treestore is only capable of storing gobject types (at this point I'm not sure because I don't know much about the gobject type system). The documentation for C lists the following, (except PixBuf) basic types which can be inserted and are automagically mapped to Python data types:

As an example, gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING, GDK_TYPE_PIXBUF); will create a new GtkTreeStore with three columns, of type int, string and GdkPixbuf respectively.

Furthermore it says you may insert any GType. The link from the documentation directly points at this paragraph:

A numerical value which represents the unique identifier of a registered type.

My research of the topic ends here and Google finds mostly GTK 2.x tutorials and nothing about inserting other data types except str and int etc.
Questions:

  • Is it possible to implement a new GType (or any other interface that will make insert custom data in a treestore possible) and how to do it?
    I already tried deriving from GObject but it didn't help.

  • How can I get rid of keeping two data structures at the same time?
    Namely my parsing result and the duplicate information in a Treestore.

  • How is it possible to deal with mixed content?
    In my case I have collections and items with different additional information (which are mirrored in the treeview as nodes with or without children).

If above questions are resolved, I also get rid of the problem when handling selections: it is difficult to match a simple type like str or int to match an item I inserted before. Such attribute has to be a key and still you would to search the list with parsed results which is quit ineffective.

Thank you in advance!

Additional information not directly related to the question:


I thought it could be a feasible challenge to implement a custom TreeModel until I read this in a tutorial for GTK 2:

However, all this comes at a cost: you are unlikely to write a useful custom model in less than a thousand lines, unless you strip all newline characters. Writing a custom model is not as difficult as it might sound though, and it may well be worth the effort, not least because it will result in much saner code if you have a lot of data to keep track of.

Is this still valid?


I just came across http://www.pygtk.org/articles/subclassing-gobject/sub-classing-gobject-in-python.htm Can this be helpful? As many resoucres it's for PyGTK 2.0. deprecated.


回答1:


Problem solved! For other people coming across the same problem, I will pile up a few helpful resources and my example code. It's okay if you know how to do it, but it's really nowhere documented.

  • Correctly deriving from GObject with properties:
    http://python-gtk-3-tutorial.readthedocs.org/en/latest/objects.html

  • How to trick a TreeView in accepting custom values for the CellRendererText, including useful snippet for the implementation of the function passed to set_cell_data_func (needs to be adapted for TreeView)
    How to make GtkListStore store object attribute in a row?

  • General good documentation on TreeViews
    http://python-gtk-3-tutorial.readthedocs.org/en/latest/treeview.html

Complete example code for having a TreeView filled with Persons and printing the selected person on a button click:

from gi.repository import Gtk
from gi.repository import GObject

class Person (GObject.GObject):
    name = GObject.property(type=str)
    age = GObject.property(type=int)
    gender = GObject.property(type=bool, default=True)

    def __init__(self):
        GObject.GObject.__init__(self)

    def __repr__(self):
        s = None
        if self.get_property("gender"): s = "m"
        else: s = "f"
        return "%s, %s, %i" % (self.get_property("name"), s, self.get_property("age"))

class MyApplication (Gtk.Window):

    def __init__(self, *args, **kwargs):
        Gtk.Window.__init__(self, *args, **kwargs)
        self.set_title("Tree Display")
        self.set_size_request(400, 400)
        self.connect("destroy", Gtk.main_quit)
        self.create_widgets()
        self.insert_rows()
        self.show_all()

    def create_widgets(self):
        self.treestore = Gtk.TreeStore(Person.__gtype__)
        self.treeview = Gtk.TreeView()
        self.treeview.set_model(self.treestore)
        column = Gtk.TreeViewColumn("Person")

        cell = Gtk.CellRendererText()
        column.pack_start(cell, True)

        column.set_cell_data_func(cell, self.get_name)

        self.treeview.append_column(column)
        vbox = Gtk.VBox()
        self.add(vbox)
        vbox.pack_start(self.treeview, True, True, 0)

        button = Gtk.Button("Retrieve element")
        button.connect("clicked", self.retrieve_element)
        vbox.pack_start(button, False, False, 5)

    def get_name(self, column, cell, model, iter, data):
        cell.set_property('text', self.treestore.get_value(iter, 0).name)

    def insert_rows(self):
        for name, age, gender in [("Tom", 19, True), ("Anna", 35, False)]:
            p = Person()
            p.name = name
            p.age = age
            p.gender = gender
            self.treestore.append(None, (p,))

    def retrieve_element(self, widget):
        model, treeiter = self.treeview.get_selection().get_selected()
        if treeiter:
            print "You selected", model[treeiter][0]

if __name__ == "__main__":
    GObject.type_register(Person)
    MyApplication()
    Gtk.main()


来源:https://stackoverflow.com/questions/11178743/gtk-3-0-how-to-use-a-gtk-treestore-with-custom-model-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!