bootstack.Window#

class bootstack.Window(*, title='', size=None, icon=None, position=None, min_size=None, max_size=None, resizable=None, modal=False, parent=None, center_on_parent=False, center_on_screen=False, topmost=False, undecorated=False, window_controls=True, window_style=None, on_close=None, padding=None, gap=0, horizontal_items='center', vertical_items='top', grow_items=False, surface=None, **kwargs)#

Bases: WindowControlsMixin, ChromeHostMixin, FlexContainer

A secondary top-level window.

Behaves as an implicit Column from the user’s perspective: children created inside a with block are automatically packed into its content frame top-to-bottom. Call show() to display the window after building its content, or block_until_closed() to show it and wait for the user to close it.

Parameters:
  • title (str) – Window title bar text.

  • size (tuple[int, int] | None) – Initial size as (width, height).

  • icon (str | Image | AppIcon | None) – Title-bar and taskbar icon — an icon file path, an Image handle, or an AppIcon. Defaults to the bootstack icon.

  • position (tuple[int, int] | None) – Initial position as (x, y).

  • min_size (tuple[int, int] | None) – Minimum window size as (width, height).

  • max_size (tuple[int, int] | None) – Maximum window size as (width, height).

  • resizable (tuple[bool, bool] | None) – Whether the window can be resized as (width, height).

  • modal (Literal[False, True, 'window', 'app']) – Modality level. False — non-modal (default). True or 'window' — grabs input from the parent only. 'app' — grabs input from all windows.

  • parent (Any) – The window this one belongs to — an App, another Window, or any widget inside one. Makes this window transient to it (stacks above it, hides with it) and is the target for center_on_parent. Defaults to the main application window.

  • center_on_parent (bool) – Center over parent (the main window if parent is not given).

  • center_on_screen (bool) – Center on the screen.

  • topmost (bool) – Keep the window above other windows.

  • undecorated (bool) – Remove OS window decorations (overrideredirect). Ignored on macOS. Unless window_controls=False, the window gets a built-in draggable title bar with min/max/close so it stays movable and closeable; add your own with add_toolbar(show_window_controls=True) to take over the chrome.

  • window_controls (bool) – When undecorated, provide a built-in title bar with window controls and dragging if you add no chrome toolbar of your own. Set False for a truly chromeless window (e.g. a splash or popover). Default True. No effect on a decorated window.

  • window_style (WindowStyle | str | None) – Windows-only window effect. None inherits the app’s setting.

  • on_close (Callable[[], bool | None] | None) – Callback invoked when the user clicks the close button. Return False to veto; return None or True to allow.

  • padding (Padding | None) – Inner padding for the content frame.

  • gap (int) – Spacing between children.

  • horizontal_items (HAlign) – Horizontal alignment of children — 'left', 'center', 'right', or 'stretch' (fill the width). Default 'center'.

  • vertical_items (VArrange) – How children are arranged top to bottom — 'top', 'center', 'bottom', or a 'space-*' mode. Default 'top'.

  • grow_items (bool) – When True, children grow equally to fill the height.

  • surface (SurfaceToken | str | None) – Surface token for the content frame background.

property result: Any#

Value set before closing, returned by block_until_closed().

property schedule: Schedule#

Scheduler tied to this widget’s lifetime.

All jobs are automatically cancelled when the widget is destroyed. First access creates the Schedule instance; subsequent accesses return the same instance.

Usage:

self.schedule.delay(500, callback)
self.schedule.every(1000, tick)
job = self.schedule.idle(refresh)
job.cancel()
property title: str#

The window’s title bar text. Assigning to it updates the title live.

add_close_handler(handler)#

Register a veto-able close handler — an alias for on_close().

Parameters:

handler (Callable[[], bool | None]) – Called with no arguments on a close request. Return False to cancel the close.

add_toolbar(*, divider=False, use_macos_menus=True, **toolbar_kwargs)#

Append a Toolbar to the window’s top chrome stack and return it.

Toolbars stack full-width, top-to-bottom, in the order added — fill each with buttons, labels, widgets, and menus (toolbar.add_menu(...)). The returned Toolbar is a context manager, so the natural form reads:

with window.add_toolbar() as tb:
    tb.add_menu("File")
    tb.add_spacer()
    tb.add_theme_toggle()
Parameters:
  • divider (bool) – Draw a hairline beneath this toolbar (default False).

  • use_macos_menus (bool) – On macOS, bridge this toolbar’s menus to the native global menu bar (in-window dropdowns hide). Default True; set False to keep its menus in-window even on macOS. No effect on Windows/Linux.

  • **toolbar_kwargs (Any) – Forwarded to Toolbar — e.g. surface, density, button_variant, show_window_controls, draggable. Window chrome defaults surface='chrome' and density='compact' (a tight strip); pass density='default' for a roomier bar.

Returns:

The Toolbar for this layer.

Return type:

Any

block_until_closed()#

Show the window and block until it is destroyed.

Returns:

The value of result at the time the window was closed.

Return type:

Any

capture(path, *, inset=0, settle=0.1)#

Save a picture of this widget to an image file.

Captures the area this widget occupies on screen — one widget, a whole window, or the entire application, depending on what you call it on. The file extension selects the format, so .png, .jpg, and .pdf all work.

The widget has to be visible. A capture reads pixels from the display, so the window is brought to the front first and a hidden or detached widget cannot be captured at all. Any always-on-top setting the window already had is left as it was found.

Pair it with a save dialog to let the user choose where it goes:

import bootstack as bs

def on_export():
    chosen = bs.ask_save_file(initial_file="dashboard.png")
    if chosen:
        app.capture(chosen)
Parameters:
  • path (str | Path) – Where to write the image. Missing parent folders are created, and the extension chooses the format.

  • inset (int) – Pixels to trim from every edge, zero or more. Use 2 on a whole window to drop the native border artifact.

  • settle (float) – Seconds to let the desktop finish repainting before the pixels are read. The default covers the common case of a dialog closing just beforehand. The interface pauses for this long, so a second click on the control that started the capture waits its turn rather than starting an overlapping one.

Returns:

The path that was written.

Raises:

BootstackError – If the widget is not visible on screen or has been scrolled out of view, if inset is negative, if no capture backend is available, or if the file cannot be written.

Return type:

Path

Added in version 0.3.0.

close()#

Close the window and exit its event loop.

For the application window this quits the loop started by run() — the natural action for a “Quit” command. It does not run the on_close veto handlers (those guard the user clicking the window’s close button), so a programmatic close() always proceeds.

destroy()#

Destroy the widget and release the resources it holds.

Removes the widget from its parent, destroys its children, and cancels any pending or repeating jobs on its schedule. After this the widget must not be used again. Destroying a container destroys everything inside it.

emit(event, *, data=None)#

Fire a named event on this widget, as if it produced the event itself.

This is how a composite widget surfaces high-level activity to its listeners, and the generic counterpart to the on_*() shorthands for firing events that have no dedicated method.

It is meant for the framework’s own events — 'change', 'select', 'input' and the rest of the data-carrying names. A handler registered through on() or an on_<event>() shorthand receives what is emitted.

Names that map onto a native event instead ('click', 'focus', 'blur', 'submit', …) are a different matter: emitting one asks the toolkit to deliver a real input event, which carries no payload and can reach handlers the widget installed for its own use. Emitting those is not a way to notify listeners, and a listener bound through on() may not see it. Drive the widget through its properties and methods instead.

Parameters:
  • event (str) – The event name, unprefixed — the same name you pass to on() or an on_<event>() shorthand (e.g. 'change', 'select').

  • data (Any) – The payload delivered to handlers. Pass the matching payload dataclass from bootstack.events — the same object an on_<event>() handler receives.

Example

from bootstack.events import ChangeEvent

field.emit("change", data=ChangeEvent(value=new_value))
guide_layout(child, **layout_kw)#

Place child into this container’s flow and snapshot its placement.

hide()#

Hide the window without destroying it.

The window is unmapped (and usually removed from the taskbar). Bring it back with show().

maximize()#

Maximize the window where the platform supports it.

minimize()#

Minimize the window to the taskbar or dock.

on(event, handler=None)#

Bind handler to event, or return a composable Stream.

With a handler — binds immediately and returns a Subscription:

sub = widget.on("change", handler)
sub.cancel()

Without a handler — returns a Stream for operator chaining. The Tk binding is created lazily when .listen() is called:

sub = widget.on("change").debounce(300).listen(handler)
sub.cancel()
Parameters:
  • event (str) – Event name (e.g. "change", "click").

  • handler (Callable[[Any], Any] | None) – Optional callback. If omitted, a Stream is returned.

Returns:

Subscription when a handler is provided; Stream otherwise.

Return type:

Stream | Subscription

on_close(handler)#

Register a handler invoked when the user clicks the window’s close button.

Handlers run in registration order; return False from one to veto the close (the window stays open), or None/True to allow it. This guards the close-button gesture only — it is not run by the programmatic close().

Parameters:

handler (Callable[[], bool | None]) – Called with no arguments on a close request. Return False to cancel the close.

on_destroy(handler=None)#

Register a callback fired when the widget is destroyed.

Fires once, as the widget is torn down — the place to release resources the widget owns that aren’t cleaned up automatically (file handles, observers, external subscriptions). The handler receives a curated Event.

Parameters:

handler (Callable[[Event], Any] | None) – Called as the widget is destroyed. Omit to get a composable Stream.

Returns:

A cancellable Subscription when a handler is given, otherwise a Stream.

Return type:

Stream | Subscription

remove_close_handler(handler)#

Remove a close handler previously registered with on_close().

Parameters:

handler (Callable[[], bool | None]) – The same callable passed to on_close() or add_close_handler(). No-op if it was not registered.

set_fullscreen(value=True)#

Enter or leave fullscreen mode where supported.

Parameters:

value (bool) – True to enter fullscreen, False to exit. Default True.

set_topmost(value=True)#

Pin the window above all others, or release it.

Parameters:

value (bool) – True to keep the window above others, False to release. Default True.

show(*, anchor_to=None, anchor_point='center', window_point='center', offset=(0, 0), auto_flip=True)#

Show the window, optionally positioned relative to a widget.

With no arguments the window appears at its configured position (or centered on its parent). Pass anchor_to to place it relative to a widget instead — for example, to the right of a field.

Parameters:
  • anchor_to (Any) – A widget to position this window against, or one of 'screen', 'cursor', or 'parent'. When given, this overrides centering.

  • anchor_point (Anchor) – The point on the anchor target to align to — one of 'n', 's', 'e', 'w', 'ne', 'nw', 'se', 'sw', or 'center'. For example, 'e' is the target’s right edge.

  • window_point (Anchor) – The matching point on this window placed at the anchor (same value set). For example, anchor_point='e' with window_point='w' puts this window’s left edge at the target’s right edge — i.e. to its right.

  • offset (tuple[int, int]) – Extra (x, y) pixel offset from the anchored position.

  • auto_flip (bool) – Flip to the opposite side if the window would otherwise go off-screen. Defaults to True.

Returns:

self — allows chaining – win = Window(...).show().

Return type:

Window