Gtk.Switch

g GObject.GInterface GObject.GInterface Gtk.Accessible Gtk.Accessible GObject.GInterface->Gtk.Accessible Gtk.Actionable Gtk.Actionable GObject.GInterface->Gtk.Actionable Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable Gtk.ConstraintTarget Gtk.ConstraintTarget GObject.GInterface->Gtk.ConstraintTarget GObject.InitiallyUnowned GObject.InitiallyUnowned Gtk.Widget Gtk.Widget GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Accessible->Gtk.Widget Gtk.Switch Gtk.Switch Gtk.Actionable->Gtk.Switch Gtk.Buildable->Gtk.Widget Gtk.ConstraintTarget->Gtk.Widget Gtk.Widget->Gtk.Switch

Example

../_images/Switch.png
Subclasses:

None

Methods

Inherited:

Gtk.Widget (181), GObject.Object (37), Gtk.Accessible (15), Gtk.Buildable (1), Gtk.Actionable (5)

Structs:

Gtk.WidgetClass (18), GObject.ObjectClass (5)

class

new ()

get_active ()

get_state ()

set_active (is_active)

set_state (state)

Virtual Methods

Inherited:

Gtk.Widget (25), GObject.Object (7), Gtk.Accessible (6), Gtk.Buildable (9), Gtk.Actionable (4)

Properties

Inherited:

Gtk.Widget (34), Gtk.Accessible (1), Gtk.Actionable (2)

Name

Type

Flags

Short Description

active

bool

r/w/en

state

bool

r/w/en

Signals

Inherited:

Gtk.Widget (13), GObject.Object (1)

Name

Short Description

activate

Emitted to animate the switch.

state-set

Emitted to change the underlying state.

Fields

Inherited:

Gtk.Widget (13), GObject.Object (1)

Class Details

class Gtk.Switch(**kwargs)
Bases:

Gtk.Widget, Gtk.Actionable

Abstract:

No

GtkSwitch is a “light switch” that has two states: on or off.

An example Gtk.Switch

The user can control which state should be active by clicking the empty area, or by dragging the handle.

GtkSwitch can also handle situations where the underlying state changes with a delay. In this case, the slider position indicates the user’s recent change (as indicated by the [property`Gtk`.Switch:active] property), and the color indicates whether the underlying state (represented by the [property`Gtk`.Switch:state] property) has been updated yet.

Gtk.Switch with delayed state change

See [signal`Gtk`.Switch::state-set] for details.

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 Gtk.AccessibleRole.SWITCH role.

classmethod new()[source]
Returns:

the newly created GtkSwitch instance

Return type:

Gtk.Widget

Creates a new GtkSwitch widget.

get_active()[source]
Returns:

True if the GtkSwitch is active, and False otherwise

Return type:

bool

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

get_state()[source]
Returns:

the underlying state

Return type:

bool

Gets the underlying state of the GtkSwitch.

set_active(is_active)[source]
Parameters:

is_active (bool) – True if self should be active, and False otherwise

Changes the state of self to the desired one.

set_state(state)[source]
Parameters:

state (bool) – the new state

Sets the underlying state of the GtkSwitch.

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

See [signal`Gtk`.Switch::state-set] for details.

Signal Details

Gtk.Switch.signals.activate(switch)
Signal Name:

activate

Flags:

RUN_FIRST, ACTION

Parameters:

switch (Gtk.Switch) – The object which received the signal

Emitted to animate the switch.

Applications should never connect to this signal, but use the [property`Gtk`.Switch:active] property.

Gtk.Switch.signals.state_set(switch, state)
Signal Name:

state-set

Flags:

RUN_LAST

Parameters:
  • switch (Gtk.Switch) – The object which received the signal

  • state (bool) – the new state of the switch

Returns:

True to stop the signal emission

Return type:

bool

Emitted to change the underlying state.

The ::state-set signal is emitted when the user changes the switch position. The default handler keeps the state in sync with the [property`Gtk`.Switch:active] property.

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

Visually, the underlying state is represented by the trough color of the switch, while the [property`Gtk`.Switch:active] property is represented by the position of the switch.

Property Details

Gtk.Switch.props.active
Name:

active

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Gtk.Switch.props.state
Name:

state

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The backend state that is controlled by the switch.

See [signal`Gtk`.Switch::state-set] for details.