Switch

A Switch widget provides a toggle which enables or disables depending on the position of the widget. It is commonly used to indicate the status of a hardware device.

Constructor

The Switch can be constructed using:

switch = Gtk.Switch()

Methods

To retrieve the state of the Switch as a True or False value:

active = switch.get_active()

Alternatively, to set a state on the Switch programmatically:

switch.set_active(active)

If active is set to True, the Switch will be in the On position.

Properties

The height of the slider can be configured with the "slider-height" property. The default value is 22, with allowed values greater than 22:

switch.set_property("slider-height", height)

The width can also be defined via "slider-width" with allowed values greater than the default of 36:

switch.set_property("slider-width", width)

Signals

The commonly use signals of a Switch are:

"notify::active" (switch, state)

The "notify::active" signal emits when the Switch is toggled to either the on or off states.

Example

Below is an example of a Switch:

#!/usr/bin/env python3

from gi.repository import Gtk

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

        switch = Gtk.Switch()
        switch.connect("notify::active", self.on_switch_toggled)
        self.add(switch)

    def on_switch_toggled(self, switch, state):
        if switch.get_active():
            print("Switch toggled to on")
        else:
            print("Switch toggled to off")

window = Switch()
window.show_all()

Gtk.main()

Download: Switch