# Range slider

> A slider with two knobs that picks a range. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/range-slider/

:::tip[In plain words]
A bar with two handles to choose “from this much to that much”.
:::

Picks a range such as a price from 20 to 80. `value` is `(low, high)` and `on_change` receives the new pair. Pressing the track moves the nearer knob there and dragging keeps moving it; the knobs never cross.

`ui.range_slider` · Category: [Forms](https://albakore.github.io/pyzed/library/#forms) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#range_slider)

**Code:**

```python
pz.container(ui.range_slider((20, 80), on_change, min=0, max=100, step=5, show_value=True),
             width=360)
```

:::note[New]
This component was added recently. Tell us how it works for you in the [issue tracker](https://github.com/albakore/pyzed/issues).
:::

## When to use

- To filter by a range: prices, dates, sizes, years.
- For a single number, use [Slider](https://albakore.github.io/pyzed/library/slider/).

## Usage

```python
import pyzed as pz
from pyzed import ui

class Shop(pz.App):
    def __init__(self):
        self.price = (20, 80)

    def view(self):
        return ui.range_slider(self.price, lambda value: setattr(self, "price", value),
                               min=0, max=500, step=5, show_value="{:.0f} €")
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in Pyzed Dark or Pyzed Light to match this site's theme, and cropped to the component so it shows at its real size. The pointer hovers, presses or types into it; a component that does not react to the pointer cycles through a few themes. With reduced motion you see the first frame. Open **Code** under a capture to see the snippet that draws it; free names such as `on_save` stand for your own callbacks.
:::

### Options

```python
pz.container(ui.range_slider((0.2, 0.7), on_change), width=320)
```

```python
pz.container(ui.range_slider((10, 60), on_change, min=0, max=100, step=1, show_value=True), width=360)
```

```python
pz.container(ui.range_slider((50, 250), on_change, min=0, max=500, step=10, show_value="{:.0f} €"), width=380)
```

```python
pz.container(ui.range_slider((30, 90), on_change, min=0, max=100, tint="success"), width=320)
```

### States and events

```python
pz.container(ui.range_slider((20, 80), on_change, min=0, max=100, disabled=True), width=320)
```

## Events

Range slider has **2 events of its own**, listed below. Like every widget it also has the **18 common events** (`click`, `double_click`, `right_click`, `mouse_enter`, `drag`, `key_down`, `resize`…), attached with `.on(...)`: see [Events](https://albakore.github.io/pyzed/guides/events/) and [Events of every component](https://albakore.github.io/pyzed/library/events/).

| Event | Handler gets | When |
| --- | --- | --- |
| `on_change` | `((low, high))` | Callable that receives the new `(low, high)`. |
| `on_release` | `()` | Sent when the knob is let go (save or apply there). |

**Handler gets** is what Pyzed passes to your callable: `()` means nothing. Any handler can also be a message (a value that is not callable), which goes to `update()`.

### Handling the events

The same events written three ways. **Imperative**: methods of the app change `self`. **Declarative**: the handlers produce messages and one `update()` decides. **Functional**: a pure function draws an immutable state and asks for changes with `set_state(...)`. See [Imperative, declarative and functional](https://albakore.github.io/pyzed/guides/code-styles/).

**Imperative:**

```python
class Shop(pz.App):
    def __init__(self):
        self.price = (20, 80)

    def set_price(self, value):          # on_change: the new (low, high)
        self.price = value

    def search(self):                    # on_release: the knob was let go
        print("search", self.price)

    def view(self):
        return ui.range_slider(self.price, self.set_price, min=0, max=100, step=5,
                               on_release=self.search, show_value=True)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Price:
    value: tuple

class Shop(pz.App):
    price = (20, 80)

    def update(self, message):
        match message:
            case Price(value):
                self.price = value

    def view(self):
        return ui.range_slider(self.price, Price, min=0, max=100, step=5)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    price: tuple = (20, 80)

def view(state, set_state):
    return ui.range_slider(state.price, lambda value: set_state(price=value), min=0, max=100, step=5)
```

## Properties and API

### `ui.range_slider`

```python
range_slider(
    value,
    on_change=None,
    *,
    min=0.0,
    max=1.0,
    step=0.01,
    min_gap=0.0,
    on_release=None,
    width='fill',
    tint='accent',
    disabled=False,
    show_value=False,
    theme=None,
)
```

A slider with two knobs that picks a range. Pressing the track moves the nearer knob there and dragging keeps moving it; the knobs never cross.

| Property | Default | Description |
| --- | --- | --- |
| `value` | *required* | `(low, high)`. |
| `min` | `0.0` | Smallest value. |
| `max` | `1.0` | Largest value. |
| `step` | `0.01` | Values snap to multiples of this. |
| `min_gap` | `0.0` | Smallest distance kept between both knobs. |
| `width` | `"fill"` | Width: pixels, `"fill"`, `"shrink"` or `"fill:N"` (proportion). |
| `tint` | `"accent"` | Color of the range between the knobs. |
| `disabled` | `False` | Disables it (ignores clicks). |
| `show_value` | `False` | Adds `low – high` next to it: `True`, or a format for each end such as `"{:.0f} €"`. |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

Events: `on_change`, `on_release` ([events](#events)).

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#range_slider)

## Related

- [Slider](https://albakore.github.io/pyzed/library/slider/): Pick a number in a range by dragging.
- [Number field](https://albakore.github.io/pyzed/library/number-field/): A number with −/+ buttons, limits, step and suffix.
