Skip to content

switch

pdwidgets.widgets.switch

On/off switch widget.

Switch(parent, x=0, y=0, w=None, h=None, align=None, align_to=None, fg=None, bg=None, visible=True, value=False, padding=None, on_color=None, off_color=None, knob_color=None)

Bases: Widget

Pill track with sliding knob for boolean on/off.

Initialize a Switch: an iOS-style sliding on/off toggle.

A rounded "pill" track with a circular knob that sits left (off) or right (on); tapping anywhere on it flips the state. This is a visual alternative to the icon-swapping :class:ToggleButton, built from the same cheap round_rect + circle primitives as :class:Slider.

Parameters:

Name Type Description Default
parent Widget

The parent widget or screen that contains this switch.

required
x int

The x-coordinate of the switch.

0
y int

The y-coordinate of the switch.

0
w int

The width of the switch (defaults to twice the height).

None
h int

The height of the switch.

None
align int

The alignment of the switch.

None
align_to Widget

The widget to align to.

None
fg int

The foreground color of the switch.

None
bg int

The background color behind the switch.

None
visible bool

The visibility of the switch.

True
value bool

The initial state (default False / off).

False
padding tuple

The padding on each side of the switch.

None
on_color int

Track color when on; defaults to success.

None
off_color int

Track color when off; defaults to tertiary.

None
knob_color int

Knob color; defaults to on_primary.

None
Usage

wifi = Switch(card, align=pd.ALIGN.RIGHT, value=True) wifi.set_change_cb(lambda s: print("wifi", s.value))

draw(_=None)

Draw the pill track and the knob at the on/off position.

toggle(data=None, event=None)

Flip the switch between on and off.