Skip to content
Toga
0.5.3.dev143+gabcc6cdb6

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.

/reference/images/slider-cocoa.png

/reference/images/slider-gtk.png

/reference/images/slider-winforms.png

/reference/images/slider-android.png

/reference/images/slider-iOS.png

/reference/images/slider-web.png

Not supported

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(min=-5, max=10, value=7, on_change=my_callback)

# Discrete slider, accepting the values [0, 1.5, 3, 4.5, 6, 7.5].
toga.Slider(min=0, max=7.5, tick_count=6)

Reference

toga.Slider

Slider(
    id=None,
    style=None,
    value=None,
    min=0.0,
    max=1.0,
    tick_count=None,
    on_change=None,
    on_press=None,
    on_release=None,
    enabled=True,
    **kwargs,
)

Bases: Widget

Create a new Slider widget.

PARAMETER DESCRIPTION
id

The ID for the widget.

TYPE: str | None DEFAULT: None

style

A style object. If no style is provided, a default style will be applied to the widget.

TYPE: StyleT | None DEFAULT: None

value

Initial value of the slider. Defaults to the mid-point of the range.

TYPE: float | None DEFAULT: None

min

Initial minimum value of the slider. Defaults to 0.

TYPE: float DEFAULT: 0.0

max

Initial maximum value of the slider. Defaults to 1.

TYPE: float DEFAULT: 1.0

tick_count

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

TYPE: int | None DEFAULT: None

on_change

Initial on_change handler.

TYPE: OnChangeHandler | None DEFAULT: None

on_press

Initial on_press handler.

TYPE: OnPressHandler | None DEFAULT: None

on_release

Initial on_release handler.

TYPE: OnReleaseHandler | None DEFAULT: None

enabled

Whether the user can interact with the widget.

TYPE: bool DEFAULT: True

kwargs

Initial style properties.

DEFAULT: {}

max property writable

max

Maximum allowed value.

When setting this property, the current value and min will be clipped against the new maximum value.

min property writable

min

Minimum allowed value.

When setting this property, the current value and max will be clipped against the new minimum value.

on_change property writable

on_change

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.

on_press property writable

on_press

Handler to invoke when the user presses the slider before changing it.

on_release property writable

on_release

Handler to invoke when the user releases the slider after changing it.

tick_count property writable

tick_count

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 DESCRIPTION
ValueError

If set to a count which is not at least 2 (for the min and max). /// note | Note On iOS, tick marks are not currently displayed, but discrete mode will otherwise work correctly. ///

tick_step property

tick_step

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.

tick_value property writable

tick_value

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 DESCRIPTION
ValueError

If set to anything inconsistent with the rules above.

value property writable

value

Current value.

If the slider is discrete, setting the value will round it to the nearest tick.

RAISES DESCRIPTION
ValueError

If set to a value which is outside of the range.

toga.widgets.slider.OnChangeHandler

Bases: Protocol

__call__

__call__(widget, **kwargs)

A handler to invoke when the value is changed.

PARAMETER DESCRIPTION
widget

The Slider that was changed.

TYPE: Slider

kwargs

Ensures compatibility with arguments added in future versions.

TYPE: Any DEFAULT: {}

toga.widgets.slider.OnPressHandler

Bases: Protocol

__call__

__call__(widget, **kwargs)

A handler to invoke when the slider is pressed.

PARAMETER DESCRIPTION
widget

The Slider that was pressed.

TYPE: Slider

kwargs

Ensures compatibility with arguments added in future versions.

TYPE: Any DEFAULT: {}

toga.widgets.slider.OnReleaseHandler

Bases: Protocol

__call__

__call__(widget, **kwargs)

A handler to invoke when the slider is pressed.

PARAMETER DESCRIPTION
widget

The Slider that was released.

TYPE: Slider

kwargs

Ensures compatibility with arguments added in future versions.

TYPE: Any DEFAULT: {}