Gtk.Switch

g Atk.ImplementorIface Atk.ImplementorIface Gtk.Widget Gtk.Widget Atk.ImplementorIface->Gtk.Widget GObject.GInterface GObject.GInterface GObject.GInterface->Atk.ImplementorIface Gtk.Actionable Gtk.Actionable GObject.GInterface->Gtk.Actionable Gtk.Activatable Gtk.Activatable GObject.GInterface->Gtk.Activatable Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable GObject.InitiallyUnowned GObject.InitiallyUnowned GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Switch Gtk.Switch Gtk.Actionable->Gtk.Switch Gtk.Activatable->Gtk.Switch Gtk.Buildable->Gtk.Widget Gtk.Widget->Gtk.Switch

Example

../_images/Switch.png
Subclasses:

None

Methods

Inherited:

Gtk.Widget (278), GObject.Object (37), Gtk.Buildable (10), Gtk.Actionable (5), Gtk.Activatable (6)

Structs:

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

class

new ()

get_active ()

get_state ()

set_active (is_active)

set_state (state)

Virtual Methods

Inherited:

Gtk.Widget (82), GObject.Object (7), Gtk.Buildable (10), Gtk.Actionable (4), Gtk.Activatable (2)

do_activate ()

do_state_set (state)

Properties

Inherited:

Gtk.Widget (39), Gtk.Actionable (2), Gtk.Activatable (2)

Name

Type

Flags

Short Description

active

bool

r/w/en

Whether the switch is on or off

state

bool

r/w/en

The backend state

Style Properties

Inherited:

Gtk.Widget (17)

Name

Type

Default

Flags

Short Description

slider-height

int

22

d/r

The minimum height of the handle deprecated

slider-width

int

36

d/r

The minimum width of the handle deprecated

Signals

Inherited:

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

Name

Short Description

activate

The ::activate signal on Gtk.Switch is an action signal and emitting it causes the switch to animate.

state-set

The ::state-set signal on Gtk.Switch is emitted to change the underlying state.

Fields

Inherited:

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

Name

Type

Access

Description

parent_instance

Gtk.Widget

r

Class Details

class Gtk.Switch(**kwargs)
Bases:

Gtk.Widget, Gtk.Actionable, Gtk.Activatable

Abstract:

No

Structure:

Gtk.SwitchClass

Gtk.Switch is a widget that has two states: on or off. The user can control which state should be active by clicking the empty area, or by dragging the handle.

Gtk.Switch can also handle situations where the underlying state changes with a delay. See Gtk.Switch ::state-set for details.

CSS nodes
switch
╰── slider

Gtk.Switch has two css nodes, the main node with the name switch and a subnode named slider. Neither of them is using any style classes.

classmethod new()[source]
Returns:

the newly created Gtk.Switch instance

Return type:

Gtk.Widget

Creates a new Gtk.Switch widget.

New in version 3.0.

get_active()[source]
Returns:

True if the Gtk.Switch is active, and False otherwise

Return type:

bool

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

New in version 3.0.

get_state()[source]
Returns:

the underlying state

Return type:

bool

Gets the underlying state of the Gtk.Switch.

New in version 3.14.

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.

New in version 3.0.

set_state(state)[source]
Parameters:

state (bool) – the new state

Sets the underlying state of the Gtk.Switch.

Normally, this is the same as Gtk.Switch :active, unless the switch is set up for delayed state changes. This function is typically called from a Gtk.Switch ::state-set signal handler.

See Gtk.Switch ::state-set for details.

New in version 3.14.

do_activate() virtual
do_state_set(state) virtual
Parameters:

state (bool) –

Return type:

bool

Signal Details

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

activate

Flags:

RUN_FIRST, ACTION

Parameters:

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

The ::activate signal on Gtk.Switch is an action signal and emitting it causes the switch to animate. Applications should never connect to this signal, but use the notify::active signal.

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

The ::state-set signal on Gtk.Switch is emitted to change the underlying state. It is emitted when the user changes the switch position. The default handler keeps the state in sync with the Gtk.Switch :active property.

To implement delayed state change, applications can connect to this signal, initiate the change of the underlying state, and call 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 Gtk.Switch :active property is represented by the position of the switch.

New in version 3.14.

Property Details

Gtk.Switch.props.active
Name:

active

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether the Gtk.Switch 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 Gtk.Switch ::state-set for details.

New in version 3.14.