Search code examples
pythongtkpygtkgtktreeview

trouble setting up GtkTreeViews in PyGtk


I've got some code in a class that extends gtk.TreeView, and this is the init method. I want to create a tree view that has 3 columns. A toggle button, a label, and a drop down box that the user can type stuff into. The code below works, except that the toggle button doesn't react to mouse clicks and the label and the ComboEntry aren't drawn. (So I guess you can say it doesn't work). I can add rows just fine however.

    #make storage                   enable/disable  label    user entry
    self.tv_store = gtk.TreeStore(gtk.ToggleButton, str, gtk.ComboBoxEntry)
    #make widget
    gtk.TreeView.__init__(self, self.tv_store)
    #make renderers
    self.buttonRenderer = gtk.CellRendererToggle()
    self.labelRenderer = gtk.CellRendererText()
    self.entryRenderer = gtk.CellRendererCombo()
    #make columns

    self.columnButton = gtk.TreeViewColumn('Enabled')
    self.columnButton.pack_start(self.buttonRenderer, False)
    self.columnLabel = gtk.TreeViewColumn('Label')
    self.columnLabel.pack_start(self.labelRenderer, False)
    self.columnEntry = gtk.TreeViewColumn('Data')
    self.columnEntry.pack_start(self.entryRenderer, True)

    self.append_column(self.columnButton)
    self.append_column(self.columnLabel)
    self.append_column(self.columnEntry)

    self.tmpButton = gtk.ToggleButton('example')
    self.tmpCombo = gtk.ComboBoxEntry(None)
    self.tv_store.insert(None, 0, [self.tmpButton, 'example label', self.tmpCombo])

Solution

  • First of all, you need to create a model with bool, str and str columns, not the way you are doing now. Second, you need to bind properties of renderers from appropriate model columns, e.g. as in

    self.columnButton = \
        gtk.TreeViewColumn ('Enabled', self.buttonRenderer, 
                            active = 0)  # 0 is the tree store column index
    

    Then you need to set editable property on the renderer to True. And finally, you need to handle signals (changed or editing-done, depending on renderer type) yourself and update the store accordingly.

    It may be easier to use some helpers, e.g. Py-gtktree — there's even an example for editing a tree there.