textual.binding
This module contains the Binding
class and related objects.
See bindings in the guide for details.
BindingIDString
module-attribute
¶
BindingIDString = str
The ID of a Binding defined somewhere in the application.
Corresponds to the id
parameter of the Binding
class.
BindingType
module-attribute
¶
The possible types of a binding found in the BINDINGS
class variable.
KeyString
module-attribute
¶
KeyString = str
A string that represents a key binding.
For example, "x", "ctrl+i", "ctrl+shift+a", "ctrl+j,space,x", etc.
Keymap
module-attribute
¶
Keymap = Mapping[BindingIDString, KeyString]
A mapping of binding IDs to key strings, used for overriding default key bindings.
ActiveBinding
¶
Bases: NamedTuple
Information about an active binding (returned from active_bindings).
enabled
instance-attribute
¶
Is the binding enabled? (enabled bindings are typically rendered dim)
Binding
dataclass
¶
Binding(
key,
action,
description="",
show=True,
key_display=None,
priority=False,
tooltip="",
id=None,
)
The configuration of a key binding.
id
class-attribute
instance-attribute
¶
ID of the binding. Intended to be globally unique, but uniqueness is not enforced.
If specified in the App's keymap then Textual will use this ID to lookup the binding,
and substitute the key
property of the Binding with the key specified in the keymap.
key
instance-attribute
¶
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
¶
How the key should be shown in footer.
If None, the display of the key will use the result of App.get_key_display
.
If overridden in a keymap then this value is ignored.
priority
class-attribute
instance-attribute
¶
Enable priority binding for this key.
make_bindings
classmethod
¶
make_bindings(bindings)
Convert a list of BindingType (the types that can be specified in BINDINGS) into an Iterable[Binding].
Compound bindings like "j,down" will be expanded into 2 Binding instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Iterable[BindingType]
|
An iterable of BindingType. |
required |
Returns:
Type | Description |
---|---|
Iterable[Binding]
|
An iterable of Binding. |
parse_key
¶
with_key
¶
with_key(key, key_display=None)
Return a new binding with the key and key_display set to the specified values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The new key to set. |
required |
|
str | None
|
The new key display to set. |
None
|
Returns:
Type | Description |
---|---|
Binding
|
A new binding with the key set to the specified value. |
BindingsMap
¶
BindingsMap(bindings=None)
Manage a set of bindings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
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
.
key_to_bindings
instance-attribute
¶
Mapping of key (e.g. "ctrl+a") to list of bindings for that key.
apply_keymap
¶
apply_keymap(keymap)
Replace bindings for keys that are present in keymap
.
Preserves existing bindings for keys that are not in keymap
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Keymap
|
A keymap to overlay. |
required |
Returns:
Name | Type | Description |
---|---|---|
KeymapApplyResult |
KeymapApplyResult
|
The result of applying the keymap, including any clashed bindings. |
bind
¶
bind(
keys,
action,
description="",
show=True,
key_display=None,
priority=False,
)
Bind keys to an action.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The keys to bind. Can be a comma-separated list of keys. |
required |
|
str
|
The action to bind the keys to. |
required |
|
str
|
An optional description for the binding. |
''
|
|
bool
|
A flag to say if the binding should appear in the footer. |
True
|
|
str | None
|
Optional string to display in the footer for the key. |
None
|
|
bool
|
Is this a priority binding, checked form app down to focused widget? |
False
|
merge
classmethod
¶
merge(bindings)
Merge a bindings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Iterable[BindingsMap]
|
A number of bindings. |
required |
Returns:
Type | Description |
---|---|
BindingsMap
|
New |
KeymapApplyResult
¶
Bases: NamedTuple
The result of applying a keymap.
clashed_bindings
instance-attribute
¶
A list of bindings that were clashed and replaced by the keymap.