Switch#

A clickable button with two stable states: True (on, checked); and False (off, unchecked). The button has a text label.

../../../_images/Switch.jpeg
Availability (Key)#

macOS

GTK

Windows

iOS

Android

Web

Usage#

import toga

switch = toga.Switch()

# What is the current state of the switch?
print(f"The switch is {switch.value}")

Notes#

  • The button and the label are considered a single widget for layout purposes.

  • The visual appearance of a Switch is not guaranteed. On some platforms, it will render as a checkbox. On others, it will render as a physical “switch” whose position (and color) indicates if the switch is active. When rendered as a checkbox, the label will appear to the right of the checkbox. When rendered as a switch, the label will be left-aligned, and the switch will be right-aligned.

  • On macOS, the text color of the label cannot be set directly; any color style directive will be ignored.

Reference#

class toga.widgets.switch.Switch(text, id=None, style=None, on_change=None, value=False, enabled=True)#

Create a new Switch widget.

Inherits from Widget.

Parameters:
  • text – The text to display beside the switch.

  • id – The ID for the widget.

  • style – A style object. If no style is provided, a default style will be applied to the widget.

  • on_change – A handler that will be invoked when the switch changes value.

  • enabled – Is the switch enabled (i.e., can it be pressed?). Optional; by default, switches are created in an enabled state.

property on_change#

The handler to invoke when the value of the switch changes.

property text#

The text label for the Switch.

None, and the Unicode codepoint U+200B (ZERO WIDTH SPACE), will be interpreted and returned as an empty string. Any other object will be converted to a string using str().

Only one line of text can be displayed. Any content after the first newline will be ignored.

toggle()#

Reverse the current value of the switch.

property value#

The current state of the switch, as a Boolean.

Any non-Boolean value will be converted to a Boolean.