CellRendererToggle

When using a CellRendererToggle, it allows a widget similar to a CheckButton or RadioButton to be displayed within the TreeView.

Note

See the CellRenderer page for additional methods available.

Constructor

The CellRendererToggle can be constructed using the following:

cellrenderertoggle = Gtk.CellRendererToggle()

Methods

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

cellrenderertoggle.set_property(property, value)

By default, the CellRendererToggle is drawn as a CheckButton. This can be changed to a RadioButton using:

cellrenderertoggle.set_radio(radio)

When radio is set to True, the RadioButton style is drawn.

To make a CellRendererToggle set as active:

cellrenderertoggle.set_active(active)

When the active parameter is set to True, the CellRendererToggle will be shown in the ticked (active) state.

To prevent a CellRendererToggle from being activated:

cellrenderertoggle.set_activatable(activatable)

Properties

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

cellrendererspin.set_property("item", value)

The property items available for use with the CellRendererSpin are:

  • "activatable" - customise whether the CellRendererToggle is activatable.
  • "active" - this toggles the state of the CellRendererToggle.
  • "inconsistent" - when set to True, the CellRendererToggle can be used to indicate the status of other features.
  • "radio" - if set to True, the CellRendererToggle will be drawn like a RadioButton.

Signals

The commonly used signals of a CellRendererToggle are:

"toggled" (cellrenderertoggle, path)

The "toggled" signals emits from the CellRendererToggle when the user clicks to display or remove the tick within the widget. It provides the path which identifies the location of the item which has been modified.

Example

Below is an example of a CellRendererToggle:

#!/usr/bin/env python3

from gi.repository import Gtk

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

        self.liststore = Gtk.ListStore(str, bool)
        self.liststore.append(["Ethernet", True])
        self.liststore.append(["Wireless", True])
        self.liststore.append(["Bluetooth", False])
        self.liststore.append(["3g Mobile", True])

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

        cellrenderertext = Gtk.CellRendererText()

        cellrenderertoggle = Gtk.CellRendererToggle()
        cellrenderertoggle.connect("toggled", self.on_cell_toggled)

        treeviewcolumn = Gtk.TreeViewColumn("Connection Type")
        treeviewcolumn.pack_start(cellrenderertext, False)
        treeviewcolumn.add_attribute(cellrenderertext, "text", 0)
        treeview.append_column(treeviewcolumn)

        treeviewcolumn = Gtk.TreeViewColumn("Status")
        treeviewcolumn.pack_start(cellrenderertoggle, False)
        treeviewcolumn.add_attribute(cellrenderertoggle, "active", 1)
        treeview.append_column(treeviewcolumn)

    def on_cell_toggled(self, cellrenderertoggle, treepath):
        self.liststore[treepath][1] = not self.liststore[treepath][1]

window = CellRendererToggle()
window.show_all()

Gtk.main()

Download: CellRendererToggle