Skip to main content

Toggle

A toggle represents a physical switch that allows someone to choose between two mutually exclusive options. For example, "On/Off", "Show/Hide". Choosing an option should produce an immediate result.

Examples​

Live demo

Basic toggles​

import pglet
from pglet import Toggle, Button, Text
with pglet.page("basic-toggles") as page:
def button_clicked(e):
t.value = f"Toggles values are: {t1.value}, {t2.value}, {t3.value}, {t4.value}."
page.update()

t = Text()
t1 = Toggle(label='Enabled and checked', value=True)
t2 = Toggle(label='Enabled and unchecked')
t3 = Toggle(disabled=True, label='Disabled and checked', value=True)
t4 = Toggle(disabled=True, label='Disabled and unchecked')
b = Button(text='Submit', on_click=button_clicked)
page.add(t1, t2, t3, t4, b, t)

input()

Toggles with inline labels​

import pglet
from pglet import Toggle
with pglet.page("toggles-with-inline-labels") as page:

page.add(
Toggle(inline=True, label='With inline label', on_text='On', off_text='Off'),
Toggle(disabled=True, inline=True, label='Disabled with inline label', on_text='On', off_text='Off'),
Toggle(inline=True, label='With inline label and without onText and offText'),
Toggle(disabled=True, inline=True, label='Disabled with inline label and without onText and offText'))

Toggle with change event​

import pglet
from pglet import Toggle
with pglet.page("toggle-with-change-event") as page:
def toggle_changed(e):
if t.value:
page.theme = 'dark'
else:
page.theme = 'light'
page.update()

t = Toggle(label="With 'change' event", on_text="Dark theme", off_text="Light theme", value=False, on_change=toggle_changed)

page.theme = 'light'
page.add(t)

input()

Properties​

NameTypeDefaultDescription
valueboolfalseCurrent value of the toggle.
labelstringA label for the toggle.
inlineboolfalseWhether the label (not the onText/offText) should be positioned inline with the toggle control.
onTextstringText to display when toggle is ON. Caution: when not providing on/off text user may get confused in differentiating the on/off states of the toggle.
offTextstringText to display when toggle is OFF. Caution: when not providing on/off text user may get confused in differentiating the on/off states of the toggle.
focusedboolfalseWhen set to true the focus is set on the control when it's shown on the page or page opened.
datastringAdditional data attached to the control. The value is passed in change event data along with a toggle state.

Events​

NameDescription
changeFires when the state of toggle is changed.
focusFires when the control has received focus.
blurFires when the control has lost focus.