Search code examples
pythongtkpygtkgtktreeview

Can I add a column to an existing treemodel in gtk?


I have a treeview that is populated from a treemodel.

I would like to add a colum to the treeview. Is it possible to draw the data for that column from a separate treemodel or can I append at runtime a column to the existing treemodel?


Solution

  • You can append as many columns to the tree view as you need, without the limit of the columns of the model. If the data you need are not present in the model, you can set a callback for a column:

    import gtk
    
    
    def inIta(col, cell, model, iter, mymodel):
        s = model.get_string_from_iter(iter)
        niter = mymodel.get_iter_from_string(s)
        obj = mymodel.get_value(niter, 0)
        cell.set_property('text', obj)
    
    
    model = gtk.ListStore(str)
    model2 = gtk.ListStore(str)
    view = gtk.TreeView(model)
    rend1 = gtk.CellRendererText()
    col1 = gtk.TreeViewColumn('hello', rend1, text=0)
    view.append_column(col1)
    rend2 = gtk.CellRendererText()
    col2 = gtk.TreeViewColumn('ciao', rend2)
    col2.set_cell_data_func(rend2, inIta, model2)
    view.append_column(col2)
    
    model.append(['hello world'])
    model2.append(['ciao mondo'])
    
    win = gtk.Window()
    win.connect('delete_event', gtk.main_quit)
    win.add(view)
    win.show_all()
    gtk.main()