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.
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,
)
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)
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 direction: |
|
Grow to consume extra space in the parent. |
|
Alignment when the widget does not fill the available slot:
|
|
External spacing in pixels. Accepts an integer (equal on all
sides), a 2-tuple |
|
Horizontal external spacing (left and right). Accepts an integer
or a 2-tuple |
|
Vertical external spacing (top and bottom). Accepts an integer
or a 2-tuple |
Grid#
Used inside a Grid container.
|
Zero-based row and column indices. |
|
Number of rows or columns to span. |
|
Alignment and fill within the grid cell. Any combination of
|
|
External spacing in pixels. Accepts an integer, a 2-tuple
|
|
Horizontal external spacing. Accepts an integer or |
|
Vertical external spacing. Accepts an integer or |
See also#
TextField — plain single-line text input
NumberField — numeric-only input with stepper
API#
The complete reference for PathField lives on the
Widgets API page. At a glance:
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()