ToggleSwitch

A modern on/off switch — a themed checkbutton drawn as a sliding toggle. Use it where a setting takes effect immediately (Wi-Fi on/off); use a CheckButton where the user ticks choices and confirms them later.

Live preview

Try the Theme switcher in the toolbar — this preview restyles live:

Live preview. In the Player's built-in docs viewer (scriptweaver --help), this renders ToggleSwitch as a live, theme-aware widget you can interact with.

Example

const wifi = new BoolVar({ value: true });

app.ToggleSwitch({
  text: 'Wi-Fi',
  color: 'success',
  variable: wifi,
  onClick: () => console.log('Wi-Fi:', wifi.value),
}).pack.configure({ anchor: 'w', padx: 16, pady: 8 });

Creating

const sw1 = parent.ToggleSwitch(options);

parent is any container. Besides the options below, a ToggleSwitch accepts everything a CheckButton does (variable, onValue/offValue, image, state, …) — underneath it is a themed checkbutton, so the value model is identical.

Options

Option Type Description
text string The label next to the switch.
color string Semantic color of the "on" state: primary (default), secondary, success, info, warning, danger.
shape string 'round' (default, a pill) or 'square'.
variable BoolVar Two-way binding for the on/off state.
onClick function Called when the switch is flipped (mouse, Space, or invoke()).
style string Escape hatch: a full ttk style name; overrides color/shape. bootstyle works too.

color and shape can also be read and assigned at runtime:

sw1.color = 'danger'; // re-skins the switch
sw1.shape = 'square';

Keyboard & accessibility

The switch takes Tab focus, shows a visible focus ring, and toggles with Space. Its default accessible role is switch; pass accessibleLabel for an explicit screen-reader name when the visible text isn't enough. See Accessibility.

Methods

Method Description
invoke() Flip the switch programmatically (runs onClick, updates variable).

ToggleSwitch also inherits the common widget methods — layout (pack, grid, place), bind(), focus(), destroy(). See Widget basics.

See also