textual.getters
Descriptors to define properties on your widget, screen, or App.
app
¶
app(app_type)
Bases: Generic[AppType]
Create a property to return the active app.
All widgets have a default app
property which returns an App instance.
Type checkers will complain if you try to access attributes defined on your App class, which aren't
present in the base class. To keep the type checker happy you can add this property to get your
specific App subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
type[AppType] | Callable[[], type[AppType]]
|
The App subclass, or a callable which returns an App subclass. |
required |
child_by_id
¶
child_by_id(child_id, expect_type=None)
Create a child_by_id property, which returns the child with the given ID.
This is similar using query_one with an id selector, except that only the immediate children are considered. It is also more efficient as it doesn't need to search the DOM.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The |
required |
|
type[QueryType] | None
|
The type of the expected widget, e.g. |
None
|
query_one
¶
query_one(selector: str, expect_type: type[QueryType])
query_one(selector, expect_type=None)
Create a query one property.
A query one property calls Widget.query_one when accessed, and returns
a widget. If the widget doesn't exist, then the property will raise the same exceptions as Widget.query_one
.
Example
from textual import getters
class MyScreen(screen):
# Note this is at the class level
output_log = getters.query_one("#output", RichLog)
def compose(self) -> ComposeResult:
with containers.Vertical():
yield RichLog(id="output")
def on_mount(self) -> None:
self.output_log.write("Screen started")
# Equivalent to the following line:
# self.query_one("#output", RichLog).write("Screen started")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str | type[QueryType]
|
A TCSS selector, e.g. "#mywidget". Or a widget type, i.e. |
required |
|
type[QueryType] | None
|
The type of the expected widget, e.g. |
None
|