:right-sidebar: True Switch =================================================================== .. currentmodule:: gi.repository.Gtk .. class:: Switch(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.Gtk.Widget`, :class:`~gi.repository.GObject.InitiallyUnowned`, :class:`~gi.repository.GObject.Object` Implemented Interfaces: :class:`~gi.repository.Gtk.Accessible`, :class:`~gi.repository.Gtk.Actionable`, :class:`~gi.repository.Gtk.Buildable`, :class:`~gi.repository.Gtk.ConstraintTarget` ``GtkSwitch`` is a "light switch" that has two states: on or off. .. image:: https://docs.gtk.org/gtk4/switch.png The user can control which state should be active by clicking the empty area, or by dragging the slider. ``GtkSwitch`` can also express situations where the underlying state changes with a delay. In this case, the slider position indicates the user's recent change (represented by the :obj:`~gi.repository.Gtk.Switch.props.active` property), while the trough color indicates the present underlying state (represented by the :obj:`~gi.repository.Gtk.Switch.props.state` property). .. image:: https://docs.gtk.org/gtk4/switch-state.png See :obj:`~gi.repository.Gtk.Switch.signals.state_set` for details. Shortcuts and Gestures -------------------------------------------------------------------------------- ``GtkSwitch`` supports pan and drag gestures to move the slider. CSS nodes -------------------------------------------------------------------------------- .. code-block:: :dedent: switch ├── image ├── image ╰── slider ``GtkSwitch`` has four css nodes, the main node with the name switch and subnodes for the slider and the on and off images. Neither of them is using any style classes. Accessibility -------------------------------------------------------------------------------- ``GtkSwitch`` uses the :const:`~gi.repository.Gtk.AccessibleRole.SWITCH` role. Constructors ------------ .. rst-class:: interim-class .. class:: Switch :no-index: .. classmethod:: new() -> ~gi.repository.Gtk.Widget Creates a new ``GtkSwitch`` widget. Methods ------- .. rst-class:: interim-class .. class:: Switch :no-index: .. method:: get_active() -> bool Gets whether the ``GtkSwitch`` is in its “on” or “off” state. .. method:: get_state() -> bool Gets the underlying state of the ``GtkSwitch``\. .. method:: set_active(is_active: bool) -> None Changes the state of ``self`` to the desired one. :param is_active: :const:`True` if ``self`` should be active, and :const:`False` otherwise .. method:: set_state(state: bool) -> None Sets the underlying state of the ``GtkSwitch``\. This function is typically called from a :obj:`~gi.repository.Gtk.Switch.signals.state_set` signal handler in order to set up delayed state changes. See :obj:`~gi.repository.Gtk.Switch.signals.state_set` for details. :param state: the new state Properties ---------- .. rst-class:: interim-class .. class:: Switch :no-index: .. attribute:: props.active :type: bool The type of the None singleton. .. attribute:: props.state :type: bool The type of the None singleton. Signals ------- .. rst-class:: interim-class .. class:: Switch.signals :no-index: .. method:: activate() -> None Emitted to animate the switch. Applications should never connect to this signal, but use the :obj:`~gi.repository.Gtk.Switch.props.active` property. .. method:: state_set(state: bool) -> bool The type of the None singleton. :param state: the new state of the switch