Switches, sliders and pickers¶
Use a switch, slider or picker when someone can choose a value in your Python script’s native interface. For a practice plan, that might be a warm-up, the length of the session, an instrument or a start time.
"""Set a start time, length and warm-up for a music practice session."""
from dataclasses import dataclass
from datetime import time
from juno import ui
@dataclass
class State:
warm_up: bool = True
minutes: float = 30
instrument: str = "piano"
start: time = time(17, 30)
state = State()
def set_warm_up(value):
state.warm_up = value
def set_minutes(value):
state.minutes = value
def set_instrument(tag):
state.instrument = tag
def set_start(value):
state.start = value
def body(s):
return ui.VStack(
ui.Text("Practice plan", font="title2", weight="bold"),
ui.Picker("Instrument", options=[("piano", "Piano"), ("cello", "Cello")],
value=s.instrument, on_change=set_instrument, style="segmented"),
ui.DatePicker("Start time", value=s.start, on_change=set_start),
ui.Toggle("Include warm-up", value=s.warm_up, on_change=set_warm_up),
ui.Text(f"{int(s.minutes)} minutes of {s.instrument}"),
ui.Slider(s.minutes, minimum=10, maximum=60, step=5,
on_change=set_minutes, accessibility_label="Practice length"),
ui.Text("Start with scales" if s.warm_up else "Start with a piece"),
spacing=12, alignment="leading", padding=20, width="fill", max_width=520)
if __name__ == "__main__":
ui.run(body, state, title="Music practice")
A switch or an amount¶
A Toggle sends True or False to on_change, while a Slider sends a number. Use the slider’s minimum, maximum and step to offer practice lengths in five-minute steps. Show the amount beside the slider so people can read the value they picked.
A named choice or a time¶
Give Picker pairs such as ("cello", "Cello"): a tag for your code and a label for the person choosing. Selecting Cello sends the tag "cello" to the callback and changes the practice summary. For DatePicker, a Python date, time or timezone-free datetime selects what to edit, and the callback receives the same type.
Parameters¶
Parameter |
Default |
Meaning |
|---|---|---|
|
Required |
Switch label. |
|
Required |
Current bool from state. |
|
Required |
Function receiving a bool. |
Common keywords |
|
Parameter |
Default |
Meaning |
|---|---|---|
|
Required |
Current number between minimum and maximum. |
|
Required |
Function receiving a float. |
|
|
Lower bound; defaults to 0. |
|
|
Upper bound; defaults to 1. |
|
|
Optional positive increment. |
Common keywords |
|
Parameter |
Default |
Meaning |
|---|---|---|
|
Required |
Picker label. |
|
Required |
Nonempty iterable of unique |
|
Required |
Selected tag, which must exist in options. |
|
Required |
Function receiving a string tag. |
|
|
|
|
|
|
|
|
Boolean; show an activity indicator and block selection in menu style. |
Common keywords |
|
Parameter |
Default |
Meaning |
|---|---|---|
|
Required |
Row label. |
|
Required |
Current date, time or naive datetime from state. |
|
Required |
Function receiving a value of the same type as |
|
|
Optional earliest value, of the same type as |
|
|
Optional latest value, of the same type as |
Common keywords |
|
Seen in¶
Run Tasks from Home’s Native UI section and turn on Due Date for a task to try a switch and a date picker together.