Select¶
Added in version 0.24.0
A Select widget is a compact control to allow the user to select between a number of possible options.
- Focusable
- Container
The options in a select control may be passed in to the constructor or set later with set_options. Options should be given as a sequence of tuples consisting of two values: the first is the string (or Rich Renderable) to display in the control and list of options, the second is the value of option.
The value of the currently selected option is stored in the value
attribute of the widget, and the value
attribute of the Changed message.
Typing¶
The Select
control is a typing Generic which allows you to set the type of the option values.
For instance, if the data type for your values is an integer, you would type the widget as follows:
Note
Typing is entirely optional.
If you aren't familiar with typing or don't want to worry about it right now, feel free to ignore it.
Example¶
The following example presents a Select
with a number of options.
from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Header, Select
LINES = """I must not fear.
Fear is the mind-killer.
Fear is the little-death that brings total obliteration.
I will face my fear.
I will permit it to pass over me and through me.""".splitlines()
class SelectApp(App):
CSS_PATH = "select.css"
def compose(self) -> ComposeResult:
yield Header()
yield Select((line, line) for line in LINES)
@on(Select.Changed)
def select_changed(self, event: Select.Changed) -> None:
self.title = str(event.value)
if __name__ == "__main__":
app = SelectApp()
app.run()
Messages¶
Reactive attributes¶
Name | Type | Default | Description |
---|---|---|---|
expanded |
bool |
False |
True to expand the options overlay. |
value |
SelectType | None |
None |
Current value of the Select. |
Bindings¶
The Select widget defines the following bindings:
Key(s) | Description |
---|---|
enter,down,space,up | Activate the overlay |
textual.widgets.Select
class
¶
def __init__(
self,
options,
*,
prompt="Select",
allow_blank=True,
value=None,
name=None,
id=None,
classes=None,
disabled=False
):
Bases: Generic[SelectType]
, Vertical
Widget to select from a list of possible options.
A Select displays the current selection. When activated with Enter the widget displays an overlay with a list of all possible options.
Parameters
Name | Type | Description | Default |
---|---|---|---|
options |
Iterable[tuple[str, SelectType]]
|
Options to select from. |
required |
prompt |
str
|
Text to show in the control when no option is select. |
'Select'
|
allow_blank |
bool
|
Allow the selection of a blank option. |
True
|
value |
SelectType | None
|
Initial value (should be one of the values in |
None
|
name |
str | None
|
The name of the select control. |
None
|
id |
str | None
|
The ID of the control the DOM. |
None
|
classes |
str | None
|
The CSS classes of the control. |
None
|
disabled |
bool
|
Whether the control is disabled or not. |
False
|
BINDINGS
class-attribute
instance-attribute
¶
Key(s) | Description |
---|---|
enter,down,space,up | Activate the overlay |
expanded
class-attribute
instance-attribute
¶
True to show the overlay, otherwise False.
prompt
class-attribute
instance-attribute
¶
The prompt to show when no value is selected.
value
class-attribute
instance-attribute
¶
The value of the select.