Skip to content

Gtk.Switch

class — extends Widget, Accessible, Actionable, Buildable, ConstraintTarget

Shows a "light switch" that has two states: on or off.

<picture> <source srcset="switch-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkSwitch" src="switch.png"> </picture>

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 Switch.active property), while the trough color indicates the present underlying state (represented by the Switch.state property).

<picture> <source srcset="switch-state-dark.png" media="(prefers-color-scheme: dark)"> <img alt="GtkSwitch with delayed state change" src="switch-state.png"> </picture>

See Switch.state-set for details.

Shortcuts and Gestures

GtkSwitch supports pan and drag gestures to move the slider.

CSS nodes

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 AccessibleRole.switch role.

Constructors

new

@classmethod
def new(cls) -> Widget

Creates a new GtkSwitch widget.

Methods

get_active

def get_active(self) -> bool

Gets whether the GtkSwitch is in its “on” or “off” state.

get_state

def get_state(self) -> bool

Gets the underlying state of the GtkSwitch.

set_active

def set_active(self, is_active: bool) -> None

Changes the state of self to the desired one.

Parameters:

  • is_activeTrue if self should be active, and False otherwise

set_state

def set_state(self, state: bool) -> None

Sets the underlying state of the GtkSwitch.

This function is typically called from a Switch.state-set signal handler in order to set up delayed state changes.

See Switch.state-set for details.

Parameters:

  • state — the new state

Properties

active

active: bool  # read/write

Whether the GtkSwitch widget is in its on or off state.

state

state: bool  # read/write

The backend state that is controlled by the switch.

Applications should usually set the Switch.active property, except when indicating a change to the backend state which occurs separately from the user's interaction.

See Switch.state-set for details.

Signals

activate

def on_activate(self) -> None: ...

Emitted to animate the switch.

Applications should never connect to this signal, but use the Switch.active property.

state-set

def on_state_set(self, state: bool) -> bool: ...

Emitted to change the underlying state.

The ::state-set signal is emitted when the user changes the switch position. The default handler calls Switch.set_state with the value of state.

To implement delayed state change, applications can connect to this signal, initiate the change of the underlying state, and call Switch.set_state when the underlying state change is complete. The signal handler should return True to prevent the default handler from running.