Dialog#

Dialog creates a fully custom modal window. Provide a content_builder= function to lay out any widgets, and a buttons= list to define the footer.

Dialog — light theme Dialog — dark theme

Usage#

Content builder#

content_builder receives an empty frame. Use it as a parent for any bootstack layout or widget:

from bootstack.dialogs import Dialog

def build(frame):
    with bs.VStack(padding=24, gap=12, parent=frame):
        bs.Label("New version available", font="heading-sm")
        bs.Label("bootstack 2.1.0 is ready to install.")

dlg = Dialog(title="Update", content_builder=build)
dlg.show()

Button roles#

role= controls button styling and keyboard shortcuts automatically.

Role

Behavior

'primary'

Blue solid. Focused by default; triggered by Enter when default=True.

'secondary'

Gray solid. For neutral actions (OK, Dismiss).

'danger'

Red solid. For destructive actions. Not focused by default.

'cancel'

Gray outline. Triggered by Escape.

Reading the result#

Each DialogButton carries a result= value that dialog.result is set to when that button is clicked:

dlg = Dialog(
    title="Save changes?",
    content_builder=build,
    buttons=[
        DialogButton("Save",    role="primary", result="save",    default=True),
        DialogButton("Discard", role="danger",  result="discard"),
        DialogButton("Cancel",  role="cancel"),
    ],
)
dlg.show()

if dlg.result == "save":
    save()
elif dlg.result == "discard":
    discard()

Dialog modes#

mode= controls how the dialog interacts with the rest of the app.

Mode

Behavior

'modal'

Blocks the parent window until closed. Default.

'popover'

Closes automatically when focus leaves the dialog.

'sheet'

Like 'modal' on Windows/Linux; renders as a Cocoa sheet on macOS.

Positioning#

By default, dialogs center on the parent window. Override with anchor_to= to position relative to a widget, the cursor, or the screen:

dlg.show(anchor_to=my_button, anchor_point="s", window_point="n")

dlg.show(anchor_to="cursor")

dlg.show(position=(400, 300))

See also#

Message Dialogsalert() and confirm() for common notifications.

Form DialogFormDialog for structured data-entry forms.

Filter DialogFilterDialog for multi-select list dialogs.

API#

The complete reference for Dialog and DialogButton lives on the Dialogs API page. At a glance:

Dialog

A flexible dialog window using the builder pattern.

DialogButton

Specification for a dialog button.

Full Example#

 1
 2from bootstack.dialogs import Dialog, DialogButton
 3def show_simple():
 4    dlg = Dialog(
 5        title="Confirm deletion",
 6        content_builder=lambda frame: [
 7            bs.VStack(padding=(24, 20), gap=8, parent=frame),
 8            bs.Label("Delete 3 selected items?"),
 9            bs.Label("This action cannot be undone.", font="caption"),
10        ],
11        buttons=[
12            DialogButton("Delete", role="danger", result="delete"),
13            DialogButton("Cancel", role="cancel"),
14        ],
15    )
16    dlg.show()
17
18def show_info():
19    def build(frame):
20        with bs.VStack(padding=24, gap=12, parent=frame):
21            bs.Label("New version available", font="heading-sm")
22            bs.Label("bootstack 2.1.0 is ready to install.")
23            bs.Label("Release notes: improved themes, new widgets.", font="caption")
24
25    dlg = Dialog(
26        title="Update available",
27        content_builder=build,
28        buttons=[
29            DialogButton("Install now", role="primary", result="install", default=True),
30            DialogButton("Later",       role="cancel"),
31        ],
32        min_size=(420, 180),
33    )
34    dlg.show()
35
36def show_anchored():
37    def build(frame):
38        with bs.VStack(padding=16, gap=8, parent=frame):
39            bs.Label("Saved to Documents/report.pdf")
40
41    dlg = Dialog(
42        title=" ",
43        content_builder=build,
44        buttons=[DialogButton("OK", role="secondary", result=True, default=True)],
45        min_size=(320, 100),
46    )
47    dlg.show()
48
49with bs.App(title="Dialog", size=(680, 200), padding=20, gap=16) as app:
50
51    bs.Label("Custom Dialog", font="heading-sm")
52    with bs.HStack(gap=8):
53        bs.Button("Delete confirmation", on_click=show_simple)
54        bs.Button("Update notice",       on_click=show_info)
55        bs.Button("Simple message",      on_click=show_anchored)
56
57app.run()