Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:43:59+00:00 2026-06-06T21:43:59+00:00

I’m trying to develop a GTK application in Python and I’m really stuck with

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:44:01+00:00Added an answer on June 6, 2026 at 9:44 pm

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.