CommandBar#
A horizontal strip of buttons, labels, separators, and other widgets.
Items are added left-to-right via add_button(), add_label(),
add_separator(), add_spacer(), and add_widget().
Usage#
Separators and spacers#
add_separator() inserts a thin vertical rule between item groups.
add_spacer() inserts a flexible gap that pushes everything added after it
to the right side.
tb.add_button("Bold", icon="type-bold")
tb.add_button("Italic", icon="type-italic")
tb.add_separator()
tb.add_button("Align left", icon="text-left")
tb.add_spacer()
tb.add_button(icon="gear") # pinned to the right
Labels#
add_label() adds non-interactive text, optionally with an icon. Use it
for the application name or section titles.
tb.add_label("My App", font="heading-md")
tb.add_separator()
tb.add_button("New", icon="file-earmark-plus")
Density#
density="compact" reduces padding and button size — useful for secondary
command bars such as a rich-text formatting strip.
tb = bs.CommandBar(fill="x", density="compact")
tb.add_button(icon="type-bold")
tb.add_button(icon="type-italic")
tb.add_button(icon="type-underline")
Border and surface#
show_border=True draws a border around the command bar. surface= controls
the background token — 'card' lifts it slightly from the page background.
tb = bs.CommandBar(fill="x", show_border=True, surface="card")
tb.add_button("New", icon="file-earmark-plus")
tb.add_button("Open", icon="folder2-open")
tb.add_button("Save", icon="floppy")
Custom widgets#
Use add_widget() to embed any widget (e.g. a
Select) in the command bar. Create the
widget with the command bar as its parent first.
tb = bs.CommandBar(fill="x")
branch = bs.Select(
["main", "dev", "feat/new-ui"],
parent=tb,
)
tb.add_widget(branch)
Custom titlebar#
Set show_window_controls=True to add minimize, maximize, and close buttons
on the right, and draggable=True to let the user drag the window by the
command bar. Pair with undecorated=True on the App to remove the OS title bar.
with bs.App(title="My App", undecorated=True) as app:
tb = bs.CommandBar(
fill="x",
show_window_controls=True,
draggable=True,
)
tb.add_label("My App", font="heading-md")
tb.add_spacer()
...
app.run()
Command bar inside AppShell#
AppShell includes a built-in
command bar. Access it via shell.commandbar and call the same add_* methods.
Use command= (not on_click=) when calling add_button() on the
AppShell command bar.
with bs.AppShell(title="My App") as shell:
shell.commandbar.add_spacer()
shell.commandbar.add_button(icon="circle-half", command=bs.toggle_theme)
...
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#
AppShell —
full application scaffold with a built-in command bar, sidebar, and page stack.
Button —
standalone button widget.
API#
The complete reference for CommandBar lives on the
Widgets API page. At a glance:
A horizontal strip of buttons, labels, and other widgets. |
Full Example#
1
2with bs.App(title="CommandBar demo", minsize=(700, 300), padding=16) as app:
3
4 with bs.VStack(fill="both", expand=True, gap=16):
5
6 # ── Default density ────────────────────────────────────────────────
7 with bs.VStack(fill="x", padding=(16, 12, 16, 4)):
8 bs.Label("Default", font="heading-sm")
9
10 tb1 = bs.CommandBar(fill="x")
11 tb1.add_label("Editor", font="heading-md")
12 tb1.add_separator()
13 tb1.add_button("New", icon="file-earmark-plus")
14 tb1.add_button("Open", icon="folder2-open")
15 tb1.add_button("Save", icon="floppy")
16 tb1.add_spacer()
17 tb1.add_button(icon="circle-half", on_click=bs.toggle_theme)
18 tb1.add_button(icon="gear")
19
20 # ── Compact density ────────────────────────────────────────────────
21 with bs.VStack(fill="x", padding=(16, 12, 16, 4)):
22 bs.Label("Compact density", font="heading-sm")
23
24 tb2 = bs.CommandBar(fill="x", density="compact")
25 tb2.add_button(icon="type-bold")
26 tb2.add_button(icon="type-italic")
27 tb2.add_button(icon="type-underline")
28 tb2.add_separator()
29 tb2.add_button(icon="text-left")
30 tb2.add_button(icon="text-center")
31 tb2.add_button(icon="text-right")
32 tb2.add_separator()
33 tb2.add_button(icon="list-ul")
34 tb2.add_button(icon="list-ol")
35 tb2.add_spacer()
36 tb2.add_button(icon="arrow-counterclockwise")
37 tb2.add_button(icon="arrow-clockwise")
38
39 # ── Accents and variants ───────────────────────────────────────────
40 with bs.VStack(fill="x", padding=(16, 12, 16, 4)):
41 bs.Label("Accents and variants", font="heading-sm")
42 tb3 = bs.CommandBar(fill="x")
43 tb3.add_button("Publish", icon="cloud-upload", accent="primary")
44 tb3.add_button("Preview", icon="eye")
45 tb3.add_button("Draft", icon="pencil")
46 tb3.add_spacer()
47 tb3.add_button("Discard", icon="trash", accent="danger")
48
49 # ── Border & Surface ────────────────────────────────────────────────
50 with bs.VStack(fill="x", padding=(16, 12, 16, 4)):
51 bs.Label("Border and Surface", font="heading-sm")
52
53 tb1 = bs.CommandBar(fill="x", show_border=True, surface="card")
54 tb1.add_button("New", icon="file-earmark-plus")
55 tb1.add_button("Open", icon="folder2-open")
56 tb1.add_button("Save", icon="floppy")
57
58
59app.run()