Widgets
ui.Button
A button that calls a Python function when clicked.
class Button(View):
def __init__(
self,
*,
# The button text
label: str,
# The function to be invoked when the button is clicked.
# The function should require no arguments. Its return value is ignored.
on_click: Callable[[], None] | None
):
...
Example: a simple counter
from scripton import ui def render(state): def on_click(): state.count += 1 return ui.Button( label=f'Click Count: {state.count}', on_click=on_click ) ui.run( render, state=ui.State(count=0) )
ui.Slider
A slider component that allows you to drag and select an numeric value within a given range.
class Slider(View):
def __init__(
self,
*,
# The current value of the slider.
# Example: 20 or state.bind.foo
value: Number | Binding,
# The lower and upper bounds for the slider (both endpoints inclusive).
# Example: (0, 100)
range: Optional[tuple[Number, Number]] = None,
# The step-size/increment for the slider.
# Example: 0.1
step: Optional[Number] = None,
# A label displayed alongside the slider.
# Example: 'phase'
label: Optional[str] = None,
# If enabled, the value is automatically clamped to be within bounds.
clamp: bool = False,
# If true, the slider updates its value immediately as it is dragged.
# Otherwise, it waits for the user has finished the drag (eg: on mouse button up).
# For sliders whose value changes result in expensive computation, it may be
# desirable to set this to False.
instant: bool = True,
# An optional callback invoked when the slider's value changes.
# Typically not necessary if a binding has been used for the value.
on_change: Optional[Callable[[float], None]] = None
):
...
Example — using a
Slider
to control the drawn circle's radiusfrom scripton import ui from scripton.canvas import Canvas def render(canvas, state): # Update the canvas based on the given state canvas.clear() canvas.draw_circle( x=150, y=150, radius=state.radius, fill='#fff' ) # Show a slider for the user interface return ui.Slider( value=state.bind.radius, range=(10, 100), label='Radius' ) ui.run( render, canvas=Canvas(width=300, height=300), state=ui.State( radius=20.0 ) )
ui.Text
Displays plain text.
class Text(View):
def __init__(
self,
# The text to be displayed.
text: str
):
...
Example
from scripton import ui def render(): return ui.Text('Hello World') ui.run(render)