Slider#
A widget for selecting a value within a range. The range is shown as a horizontal line, and the selected value is shown as a draggable marker.

Usage#
A slider can either be continuous (allowing any value within the range), or discrete (allowing a fixed number of equally-spaced values). For example:
import toga
def my_callback(slider):
print(slider.value)
# Continuous slider, with an event handler.
toga.Slider(range=(-5, 10), value=7, on_change=my_callback)
# Discrete slider, accepting the values [0, 1.5, 3, 4.5, 6, 7.5].
toga.Slider(range=(0, 7.5), tick_count=6)
Reference#
- class toga.widgets.slider.Slider(id=None, style=None, value=None, range=(0.0, 1.0), tick_count=None, on_change=None, on_press=None, on_release=None, enabled=True)#
Create a new slider widget.
Inherits from
Widget
.- Parameters:
id – The ID for the widget.
style – A style object. If no style is provided, a default style will be applied to the widget.
value (float | None) – Initial
value
of the slider. Defaults to the mid-point of the range.range (tuple[float, float]) – Initial
range
range of the slider. Defaults to(0, 1)
.tick_count (int | None) – Initial
tick_count
for the slider. IfNone
, the slider will be continuous.on_change (callable | None) – Initial
on_change
handler.on_press (callable | None) – Initial
on_press
handler.on_release (callable | None) – Initial
on_release
handler.enabled (bool) – Whether the user can interact with the widget.
- property max: float#
Maximum allowed value.
This property is read-only, and depends on the value of
range
.
- property min: float#
Minimum allowed value.
This property is read-only, and depends on the value of
range
.
- property on_change: callable#
Handler to invoke when the value of the slider is changed, either by the user or programmatically.
Setting the widget to its existing value will not call the handler.
- property on_press: callable#
Handler to invoke when the user presses the slider before changing it.
- property on_release: callable#
Handler to invoke when the user releases the slider after changing it.
- property range: tuple[float, float]#
Range of allowed values, in the form (min, max).
If a range is set which doesn’t include the current value, the value will be changed to the min or the max, whichever is closest.
- Raises:
ValueError – If the min is not strictly less than the max.
- property tick_count: int | None#
Number of tick marks to display on the slider.
If this is
None
, the slider will be continuous.If this is an
int
, the slider will be discrete, and will have the given number of possible values, equally spaced within therange
.
Setting this property to an
int
will round the current value to the nearest tick.- Raises:
ValueError – If set to a count which is not at least 2 (for the min and max).
Note
On iOS, tick marks are not currently displayed, but discrete mode will otherwise work correctly.
- property tick_step: float | None#
Step between adjacent ticks.
If the slider is continuous, this property returns
None
If the slider is discrete, it returns the difference in value between adjacent ticks.
This property is read-only, and depends on the values of
tick_count
andrange
.
- property tick_value: int | None#
Value of the slider, measured in ticks.
If the slider is continuous, this property returns
None
.If the slider is discrete, it returns an integer between 1 (representing
min
) andtick_count
(representingmax
).
- Raises:
ValueError – If set to anything inconsistent with the rules above.
- property value: float#
Current value.
If the slider is discrete, setting the value will round it to the nearest tick.
- Raises:
ValueError – If set to a value which is outside of the
range
.