CellRendererText

A CellRendereText is used to display text-only within a TreeView. Besides basic text, there are a number of properties which can be used to control the look of the text including the font, style, size, foreground and background colours, and whether the text is editable.

Note

See the CellRenderer page for additional methods available.

Constructor

The CellRendererText can be constructed using the following:

cellrenderertext = Gtk.CellRendererText()

Methods

CellRenderer widgets only use one method which is used to set the styling functions of the cell:

cellrenderertext.set_property(property, value)

Properties

The configuration of the CellRendererText is made using the property functions:

cellrenderertext.set_property("item", value)

The property items available for use with the CellRendererText are:

  • "editable" - when the value set to True, allows the user to edit the text within the cell.
  • "family" - allows a family of fonts to be specified in order of preference, for example; cantarell, “droid sans”, monospace.
  • "font" - a font other than the system default can be used by specifying the name and size as the value.
  • "size" - specifies a font size in points as an integer value.
  • "strikethrough" - specifying True as the value adds a strikethrough to any text within the cell.
  • "text" - setting the value to the column number of the model indicates which column the text should be pulled from.
  • "underline" - using True will place a line beneath the text.
  • "weight" - allows configuration of letter thinness or thickness, with an integer value of 400 being standard. Higher values increase thickness, lower values decrease thickness.

Signals

The commonly used signals of a CellRendererText are:

"edited" (cellrenderertext, path, text)

An "edited" event is emitted from the CellRendererText when the user double-clicks on the cell. At this point, the cell is passed to the function along with a path identifying the location of the edited text, and finally the new text which has been entered.

Example

Below is an example of a CellRendererText:

#!/usr/bin/env python3

from gi.repository import Gtk

class CellRendererText(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect("destroy", Gtk.main_quit)

        self.liststore = Gtk.ListStore(str, str)
        self.liststore.append(["Fedora", "http://fedoraproject.org/"])
        self.liststore.append(["Ubuntu", "http://www.ubuntu.com/"])
        self.liststore.append(["Slackware", "http://www.slackware.com/"])

        treeview = Gtk.TreeView()
        treeview.set_model(self.liststore)
        self.add(treeview)

        cellrenderertext = Gtk.CellRendererText()

        treeviewcolumn = Gtk.TreeViewColumn("Distribution")
        treeviewcolumn.pack_start(cellrenderertext, True)
        treeviewcolumn.add_attribute(cellrenderertext, "text", 0)
        treeview.append_column(treeviewcolumn)

        cellrenderertext = Gtk.CellRendererText()
        cellrenderertext.set_property("editable", True)
        cellrenderertext.connect("edited", self.on_cell_edited)

        treeviewcolumn = Gtk.TreeViewColumn("Website")
        treeviewcolumn.pack_start(cellrenderertext, True)
        treeviewcolumn.add_attribute(cellrenderertext, "text", 1)
        treeview.append_column(treeviewcolumn)

    def on_cell_edited(self, cellrenderertext, treepath, text):
        self.liststore[treepath][1] = text

window = CellRendererText()
window.show_all()

Gtk.main()

Download: CellRendererText