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.

../../../_images/Slider.png
Availability (Key)#

macOS

GTK

Windows

iOS

Android

Web

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, 1), 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 – Initial value of the slider. Defaults to the mid-point of the range.

  • range – Initial range range of the slider. Defaults to (0, 1).

  • tick_count – Initial tick_count for the slider. If None, the slider will be continuous.

  • on_change – Initial on_change handler.

  • on_press – Initial on_press handler.

  • on_release – Initial on_release handler.

  • enabled – 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]#

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 the range.

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 and range.

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) and tick_count (representing max).

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.