Skip to content

textual.binding

This module contains the Binding class and related objects.

See bindings in the guide for details.

ActiveBinding

Bases: NamedTuple

Information about an active binding (returned from active_bindings).

binding instance-attribute

binding

The binding information.

enabled instance-attribute

enabled

Is the binding enabled? (enabled bindings are typically rendered dim)

node instance-attribute

node

The node where the binding is defined.

tooltip class-attribute instance-attribute

tooltip = ''

Optional tooltip shown in Footer.

Binding dataclass

Binding(
    key,
    action,
    description="",
    show=True,
    key_display=None,
    priority=False,
    tooltip="",
)

The configuration of a key binding.

action instance-attribute

action

Action to bind to.

description class-attribute instance-attribute

description = ''

Description of action.

key instance-attribute

key

Key to bind. This can also be a comma-separated list of keys to map multiple keys to a single action.

key_display class-attribute instance-attribute

key_display = None

How the key should be shown in footer.

priority class-attribute instance-attribute

priority = False

Enable priority binding for this key.

show class-attribute instance-attribute

show = True

Show the action in Footer, or False to hide.

tooltip class-attribute instance-attribute

tooltip = ''

Optional tooltip to show in footer.

parse_key

parse_key()

Parse a key in to a list of modifiers, and the actual key.

Returns:

Type Description
tuple[list[str], str]

A tuple of (MODIFIER LIST, KEY).

BindingError

Bases: Exception

A binding related error.

BindingsMap

BindingsMap(bindings=None)

Manage a set of bindings.

Parameters:

Name Type Description Default

bindings

Iterable[BindingType] | None

An optional set of initial bindings.

None
Note

The iterable of bindings can contain either a Binding instance, or a tuple of 3 values mapping to the first three properties of a Binding.

shown_keys property

shown_keys

A list of bindings for shown keys.

bind

bind(
    keys,
    action,
    description="",
    show=True,
    key_display=None,
    priority=False,
)

Bind keys to an action.

Parameters:

Name Type Description Default

keys

str

The keys to bind. Can be a comma-separated list of keys.

required

action

str

The action to bind the keys to.

required

description

str

An optional description for the binding.

''

show

bool

A flag to say if the binding should appear in the footer.

True

key_display

str | None

Optional string to display in the footer for the key.

None

priority

bool

Is this a priority binding, checked form app down to focused widget?

False

copy

copy()

Return a copy of this instance.

Return

New bindings object.

from_keys classmethod

from_keys(keys)

Construct a BindingsMap from a dict of keys and bindings.

Parameters:

Name Type Description Default

keys

dict[str, list[Binding]]

A dict that maps a key on to a list of Binding objects.

required

Returns:

Type Description
BindingsMap

New BindingsMap

get_bindings_for_key

get_bindings_for_key(key)

Get a list of bindings for a given key.

Parameters:

Name Type Description Default

key

str

Key to look up.

required

Raises:

Type Description
NoBinding

If the binding does not exist.

Returns:

Type Description
list[Binding]

A list of bindings associated with the key.

merge classmethod

merge(bindings)

Merge a bindings.

Parameters:

Name Type Description Default

bindings

Iterable[BindingsMap]

A number of bindings.

required

Returns:

Type Description
BindingsMap

New BindingsMap.

InvalidBinding

Bases: Exception

Binding key is in an invalid format.

NoBinding

Bases: Exception

A binding was not found.