PathField#

A text field with a browse button that opens a native file or directory picker dialog. When the user confirms a selection, the field value is updated and a change event fires.

PathField — light theme PathField — dark theme

Usage#

Basic usage#

bs.PathField(placeholder="Select a file…")

Dialog modes#

Use mode= to control which native dialog opens when the browse button is clicked. The default is 'open'.

bs.PathField(mode="open")           # single-file open
bs.PathField(mode="open_multiple")  # multi-file open (result is a tuple)
bs.PathField(mode="save")           # save-file dialog
bs.PathField(mode="directory")      # directory picker

File filters#

Pass file_filters= as a list of (description, pattern) pairs to restrict which file types appear in the dialog. Has no effect in 'directory' mode.

bs.PathField(
    file_filters=[
        ("Images", "*.png *.jpg *.jpeg *.gif"),
        ("All Files", "*.*"),
    ],
)

Label and message#

Use label= for a field title and message= for helper text below. Set required=True to mark the field visually.

bs.PathField(
    label="Output directory",
    message="Folder must be writable.",
    mode="directory",
    required=True,
)
PathField label and message — light theme PathField label and message — dark theme

States#

bs.PathField(value="/home/user/docs/report.pdf", label="Normal")
bs.PathField(value="/home/user/docs/report.pdf", label="Read only", read_only=True)
bs.PathField(value="/home/user/docs/report.pdf", label="Disabled",  disabled=True)
PathField states — light theme PathField states — dark theme

Reactive binding#

Bind a Signal[str] with textsignal=. The field and signal stay in sync automatically.

path = bs.Signal("")
bs.PathField(label="Pick a file", textsignal=path)
bs.Label(textsignal=path, accent="secondary")   # updates on each selection

Handling changes#

Use on_change() to respond when the user picks a path. The event fires after the dialog closes and the value is committed, or when the user edits the text portion directly and leaves the field.

pf = bs.PathField(label="Source file")

def handle_change(e):
    print("Selected:", pf.value)

pf.on_change(handle_change)

# As a subscription (cancellable)
sub = pf.on_change(handle_change)
sub.cancel()

# As a Stream (composable)
pf.on_change().listen(handle_change)

For 'open_multiple' mode, the raw tuple of paths is available on pf.dialog_result after the dialog closes.

Widget sizing#

All widgets accept self-placement kwargs via **kwargs. The parent container determines which options apply — stack-based parents use stack kwargs, grid-based parents use grid kwargs. Unrecognised keys are silently ignored.

Stack#

Used inside VStack, HStack, App, and other stack containers.

fill

Fill direction: 'x', 'y', 'both', or 'none'.

expand

Grow to consume extra space in the parent. True or False.

anchor

Alignment when the widget does not fill the available slot: 'n', 's', 'e', 'w', 'center', 'nw', etc.

margin

External spacing in pixels. Accepts an integer (equal on all sides), a 2-tuple (horizontal, vertical), or a 4-tuple (left, top, right, bottom).

margin_x

Horizontal external spacing (left and right). Accepts an integer or a 2-tuple (left, right) for asymmetric spacing. Overrides the horizontal component of margin=.

margin_y

Vertical external spacing (top and bottom). Accepts an integer or a 2-tuple (top, bottom) for asymmetric spacing. Overrides the vertical component of margin=.

Grid#

Used inside a Grid container.

row / column

Zero-based row and column indices.

rowspan / columnspan

Number of rows or columns to span.

sticky

Alignment and fill within the grid cell. Any combination of 'n', 's', 'e', 'w' — e.g. 'ew' stretches horizontally, 'nsew' fills the entire cell.

margin

External spacing in pixels. Accepts an integer, a 2-tuple (horizontal, vertical), or a 4-tuple (left, top, right, bottom).

margin_x

Horizontal external spacing. Accepts an integer or (left, right).

margin_y

Vertical external spacing. Accepts an integer or (top, bottom).

See also#

API#

The complete reference for PathField lives on the Widgets API page. At a glance:

PathField

A text field with a browse button that opens a native file/directory dialog.

Full Example#

 1
 2with bs.App(title="PathField Demo", padding=20, gap=16) as app:
 3
 4    # Basic usage
 5    bs.Label("Basic", font="heading-sm")
 6    bs.PathField(placeholder="Select a file…")
 7
 8    # Dialog modes
 9    bs.Label("Dialog Modes", font="heading-sm")
10    bs.PathField(label="Open file",        mode="open",          placeholder="Select a file…")
11    bs.PathField(label="Open multiple",    mode="open_multiple", placeholder="Select files…")
12    bs.PathField(label="Save file",        mode="save",          placeholder="Choose save location…")
13    bs.PathField(label="Select directory", mode="directory",     placeholder="Select a folder…")
14
15    # File filters
16    bs.Label("File Filters", font="heading-sm")
17    bs.PathField(
18        label="Image file",
19        file_filters=[("Images", "*.png *.jpg *.jpeg *.gif"), ("All Files", "*.*")],
20        placeholder="Select an image…",
21    )
22
23    # Label and message
24    bs.Label("Label and Message", font="heading-sm")
25    bs.PathField(
26        label="Project folder",
27        message="Must contain a pyproject.toml or setup.py.",
28        placeholder="Select a folder…",
29        mode="directory",
30        required=True,
31    )
32
33    # Reactive binding
34    bs.Label("Reactive Binding", font="heading-sm")
35    path_sig = bs.Signal("")
36    bs.PathField(label="Pick a file", textsignal=path_sig)
37    bs.Label(textsignal=path_sig, accent="secondary")
38
39    # States
40    bs.Label("States", font="heading-sm")
41    bs.PathField(value="/home/user/docs/report.pdf", label="Normal")
42    bs.PathField(value="/home/user/docs/report.pdf", label="Read only", read_only=True)
43    bs.PathField(value="/home/user/docs/report.pdf", label="Disabled",  disabled=True)
44
45    # Handling changes
46    bs.Label("Handling Changes", font="heading-sm")
47    last = bs.Signal("(none)")
48    pf = bs.PathField(label="Choose a file")
49    pf.on_change(lambda e: last.set(pf.value))
50    bs.Label(textsignal=last, accent="secondary")
51
52app.run()