bootstack.Signal#
- class bootstack.Signal(value, name=None, master=None, *, allow_empty=False, dtype=None)#
Bases:
Generic[T]A reactive value that widgets can bind to.
Holds a typed value and notifies subscribers when it changes. Read it by calling the signal, update it with
set(), derive new signals withmap(), and react to changes withsubscribe().Bind a signal to a widget by passing it as
textsignal=for text-bearing widgets, orsignal=for boolean and numeric widgets.A signal holds one type, decided by the value it is created with. Pass
allow_empty=Truewhen the value can also be empty — a field bound to it reports being cleared, where one bound to an ordinary signal silently keeps its last value. Callclear()to empty a signal. It empties toNone, except where its value lives in a widget’s own variable, which holds only strings and so empties to''. A signal that allows empty may start empty, in which case there is no value to read a type from anddtype=names it.Signals may be constructed at module level (before
bs.App()exists). The backing Tk variable is created lazily on the first widget binding and torn down when the App is destroyed, so the same signal can be reused across successive App lifecycles.- property allows_empty: bool#
Whether this signal can hold an empty value.
A signal that allows empty accepts
clear(), so a field bound to it reports being cleared instead of silently keeping its last value. Declare it at construction withbs.Signal(value, allow_empty=True).
- __call__()#
Get the current value of the signal.
- clear()#
Set the signal to its empty value and notify subscribers.
The signal must have been declared
allow_empty=True. It clears toNone, except where its value lives in a widget’s Tk variable — a variable holds only strings, so there it clears to''— and to the empty set on aset-typed signal.- Raises:
TypeError – If the signal was not declared able to be empty.
- map(transform)#
Create a derived signal that transforms this signal’s value.
The derived signal recomputes whenever this signal changes. It is held weakly, so keep a reference to it — for example, by binding it to a widget — or it will stop updating once garbage-collected.
- set(value)#
Set the signal to a new value and notify subscribers.
In general, the value must match the signals types, however, an
intmay be set on afloat-typed signal (it is widened), andNoneis accepted on a signal declaredallow_empty=True.- Parameters:
value (T) – The new value. Must match the signal’s type.
- Raises:
TypeError – If the value type does not match the signal’s type (and is not an
intwidened to afloat), or if it isNoneon a signal that was not declared able to be empty.
- subscribe(callback, *, immediate=False)#
Subscribe to value changes of this signal.
- Parameters:
- Returns:
A cancellable
Handle— call.cancel()to stop listening, or use it as a context manager to unsubscribe on exit.- Return type: