Custom CellRendererToggle for Gtk Treeview

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Custom CellRendererToggle for Gtk Treeview

Reuben Rissler
I am creating a viewer for PostgreSQL databases. A boolean column may
have True, False, or None. A CellRendererToggle can not display/edit
these three states without heavy manipulation (in fact, impossible as
far as I can tell).

So I want to create a widget like PGAdmin III used. In normal state, it
displayed True, False, or nothing. When editing, it had a togglebutton
that showed active, inactive, or inconsistent. This leads me to subclass
a Gtk.CellRenderer. However, my implementation overlaps the text because
the tree row height is too small. Furthermore, editing doesn't seem to
work. Any suggestions?

Here is the code:


import gi
gi.require_version('Gtk', '3.0')
gi.require_version('PangoCairo', '1.0')
from gi.repository import Gtk, GObject, PangoCairo
import sys

class CellRendererTog (Gtk.CellRenderer):
     __gtype_name__ = 'CellRendererToggle'
     __gproperties__ = {
         'active': ( str, # type
                     "active prop", # nick
                     "Property that holds None, False, or True", # blurb
                     GObject.PARAM_READWRITE # flags
         'editable': ( bool, # type
                     "editable prop", # nick
                     "Property that holds False or True", # blurb
                     GObject.PARAM_READWRITE # flags
     def __init__(self):

     def set_editable (self, value):
         self.set_property('editable', value)

     def do_start_editing (self, event, treeview, path, background_area,
cell_area, flags):
         if self.get_property ('editable') == False:
             print ('no edit')
         print ('edit')
         return Gtk.CellRendererToggle()

     def do_render (self, cr, widget, background_area, cell_area, flags):
         layout = PangoCairo.create_layout(cr)
         active = self.get_property('active')
         if active == None:
             active = ''
         layout.set_text(active, len(active))
         PangoCairo.update_layout (cr, layout)
         cr.move_to (cell_area.x, cell_area.y)
         PangoCairo.show_layout (cr, layout)

     def do_set_property (self, pspec, value):
         setattr(self,, value)

     def do_get_property (self, pspec):
         return getattr(self,


class GUI:
     def __init__(self):

         window = Gtk.Window()
         store = Gtk.ListStore(str)
         treeview = Gtk.TreeView(model = store)
         toggleRenderer = CellRendererTog()
         toggleColumn = Gtk.TreeViewColumn(None, toggleRenderer, active = 0)
         window.connect('destroy', Gtk.main_quit)

def main():
     app = GUI()

if __name__ == "__main__":

gtk-devel-list mailing list
[hidden email]