# Time picker

> A time field with columns of hours and minutes, in 24 or 12 hours. A Pyzed component for Python desktop GUIs, with examples and its API.

Source: https://albakore.github.io/pyzed/library/time-picker/

:::tip[In plain words]
A box for a time of day, with lists of hours and minutes to choose from.
:::

`ui.TimePicker` is a field for a time of day. Its popup has a column of hours, one of minutes (every `minute_step` minutes), seconds when the format has `ss` and AM/PM when it has `A` or `h`. ↑ and ↓ move it by one step from the keyboard. `on_change` receives a `datetime.time`.

`ui.TimePicker` · Category: [Dates and times](https://albakore.github.io/pyzed/library/#dates-and-times) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#timepicker)

**Code:**

```python
pz.row([
    pz.container(ui.TimePicker(value="14:30", on_change=on_change), width=140, height=290, align_y="start"),
    pz.container(ui.TimePicker(value="14:30", format="h:mm A", minute_step=15, on_change=on_change),
                 width=140, height=290, align_y="start"),
], spacing=24)
```

:::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

- For alarms, opening hours, meetings and reminders.
- `format="HH:mm"` is 24 hours; `"h:mm A"` is 12 hours with AM/PM; `"HH:mm:ss"` adds seconds.

## Usage

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

import datetime

class Alarm(pz.App):
    def __init__(self):
        self.at = datetime.time(7, 30)

    def view(self):
        return ui.field("Wake me at", ui.TimePicker(value=self.at, format="h:mm A",
                                                    on_change=lambda e: setattr(self, "at", e.value)))
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in One Dark or One 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.
:::

### Format

```python
ui.TimePicker(value="14:30", on_change=on_change)
```

```python
ui.TimePicker(value="14:30", format="h:mm A", on_change=on_change)
```

```python
ui.TimePicker(value="14:30:15", format="HH:mm:ss", on_change=on_change)
```

### Open

```python
ui.TimePicker(value="06:15:30", format="HH:mm:ss", on_change=on_change)
```

```python
ui.TimePicker(value="14:30", format="h:mm A", minute_step=15, on_change=on_change)
```

## Events

Time picker has **3 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/).

It is a `pz.Component`, so it also raises `on_mount` and `on_unmount`, and its handlers get a `pz.Event` whose `value` holds the data.

| Event | Handler gets | When |
| --- | --- | --- |
| `on_change` | `(event)` | Receives `event.value`: a `datetime.time` or `None`. |
| `on_open` | `(event)` | The list opened. |
| `on_close` | `(event)` | The list closed. |

**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
import datetime

class Alarm(pz.App):
    def __init__(self):
        self.at = datetime.time(7, 30)

    def changed(self, event):            # on_change: event.value is a datetime.time
        self.at = event.value

    def view(self):
        return pz.column([
            ui.TimePicker(value=self.at, format="h:mm A", on_change=self.changed),
            ui.label(f"Alarm at {ui.format_time(self.at, 'HH:mm')}"),
        ], spacing=8, width=200)
```

**Declarative:**

```python
import datetime
from dataclasses import dataclass

@dataclass
class At:
    time: datetime.time

class Alarm(pz.App):
    at = datetime.time(7, 30)

    def update(self, message):
        match message:
            case At(time):
                self.at = time

    def view(self):
        return ui.TimePicker(value=self.at, on_change=lambda e: At(e.value))
```

**Functional:**

```python
import datetime
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    at: datetime.time = datetime.time(7, 30)

def view(state, set_state):
    return ui.TimePicker(value=state.at, minute_step=15, on_change=lambda e: set_state(at=e.value))
```

## Properties and API

### `ui.TimePicker`

```python
class TimePicker
```

A component: a time field with columns of hours and minutes (and seconds, and AM/PM with a 12-hour `format`) in a popup. Once clicked, ↑/↓ change it by `minute_step`. See the [Time picker](https://albakore.github.io/pyzed/library/time-picker/) page.

| Property | Default | Description |
| --- | --- | --- |
| `value` | `None` | The time (`datetime.time`, `"14:30"` or `None`). |
| `format` | `"HH:mm"` | `"HH:mm"`, `"HH:mm:ss"`, `"h:mm A"`... |
| `placeholder` | `""` | Hint while empty. |
| `minute_step` | `5` | Minutes between the ones offered. |
| `min` | `None` | The earliest time. |
| `max` | `None` | The latest time. |
| `clearable` | `True` | Shows an x that empties it. |
| `disabled` | `False` | Greys it out. |
| `invalid` | `False` | Paints the border in the error color. |

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

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

### `ui.format_time`

```python
format_time(value, fmt='HH:mm')
```

Writes a time (or `"14:30"`) with a pattern such as `"h:mm A"`.

| Property | Default | Description |
| --- | --- | --- |
| `value` | *required* | A `datetime.time` or text. |
| `fmt` | `"HH:mm"` | The pattern. |

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

### `ui.parse_time`

```python
parse_time(text, fmt='HH:mm')
```

Reads a time: `"14:30"`, `"2:30 PM"`, `"14:30:05"`, with `fmt` first. `None` when it is not one.

| Property | Default | Description |
| --- | --- | --- |
| `text` | *required* | The text. |
| `fmt` | `"HH:mm"` | The pattern. |

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

## Related

- [Date picker](https://albakore.github.io/pyzed/library/date-picker/): A date field with a calendar that drops down, in the format you choose.
- [Number field](https://albakore.github.io/pyzed/library/number-field/): A number with −/+ buttons, limits, step and suffix.
