Switch#

macOS

GTK

Windows

iOS

Android

Web

y

y

y

y

y

The switch widget is a clickable button with two stable states, True (on, checked) and False (off, unchecked).

Usage#

import toga

input = toga.Switch()

Reference#

class toga.widgets.switch.Switch(text=NOT_PROVIDED, id=None, style=None, on_change=None, value=NOT_PROVIDED, enabled=True, factory=None, label=None, on_toggle=None, is_on=None)#

Switch widget, a clickable button with two stable states, True (on, checked) and False (off, unchecked).

Parameters:
  • text (str) – Text to be shown next to the switch.

  • id (str) – AN identifier for this widget.

  • style (Style) – An optional style object. If no style is provided then a new one will be created for the widget.

  • on_change (callable) – Function to execute when pressed.

  • value (bool) – Current on or off state of the switch.

  • enabled (bool) – Whether or not interaction with the button is possible, defaults to True.

add(*children)#

Add the provided widgets as children of this widget.

If a node already has a different parent, it will be moved over. This does nothing if a node already is a child of this node.

Raises ValueError if this widget cannot have children.

Parameters:

children – The widgets to add as children of this widget.

property app#

The App to which this widget belongs.

When setting the app for a widget, all children of this widget will be recursively assigned to the same app.

Raises ValueError if the widget is already associated with another app.

property can_have_children#

Determine if the node can have children.

This does not resolve whether there actually are any children; it only confirms whether children are theoretically allowed.

property children#

The children of this node. This always returns a list, even if the node is a leaf and cannot have children.

Returns:

A list of the children for this widget.

property enabled#

Is the widget currently enabled? i.e., can the user interact with the widget?

focus()#

Set this widget to have the current input focus.

property id#

The node identifier. This id can be used to target styling directives.

insert(index, child)#

Insert a widget as a child of this widget.

If the node already has a parent, ownership of the widget will be transferred.

Raises ValueError if this node cannot have children.

Parameters:
  • index – The position in the list of children where the new widget should be added.

  • child – The child to insert as a child of this node.

property is_on#

Button Off/On state.

DEPRECATED: renamed as value

Returns:

True if on and False if the switch is off.

property label#

Button Off/On state.

DEPRECATED: renamed as text

Returns:

True if on and False if the switch is off.

property on_change#

The callable function for when the switch is pressed.

Returns:

The callable on_change function.

property on_toggle#

The callable function for when the switch is pressed.

DEPRECATED: renamed as on_change

Returns:

The callable on_toggle function.

property parent#

The parent of this node.

Returns:

The parent of this node. Returns None if this node is the root node.

refresh()#

Refresh the layout and appearance of the tree this node is contained in.

refresh_sublayouts()#
remove(*children)#

Remove the provided widgets as children of this node.

This does nothing if a given node is not a child of this node.

Raises ValueError if this node is a leaf, and cannot have children.

Parameters:

children – The child nodes to remove.

property root#

The root of the tree containing this node.

Returns:

The root node. Returns self if this node is the root node.

property tab_index#

The position of the widget in the focus chain for the window.

property text#

Accompanying text label of the Switch.

Returns:

The label text of the widget as a str.

toggle()#

Reverse the value of Switch.value property from true to false and vice versa.

property value#

Button Off/On state.

Returns:

True if on and False if the switch is off.

property window#

The window to which this widget belongs.

When setting the window for a widget, all children of this widget will be recursively assigned to the same window.