MCPcopy
hub / github.com/Textualize/textual / query_one

Class query_one

src/textual/getters.py:62–150  ·  view source on GitHub ↗

Create a query one property. A query one property calls [Widget.query_one][textual.dom.DOMNode.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: ```python from t

Source from the content-addressed store, hash-verified

60
61
62class query_one(Generic[QueryType]):
63 """Create a query one property.
64
65 A query one property calls [Widget.query_one][textual.dom.DOMNode.query_one] when accessed, and returns
66 a widget. If the widget doesn't exist, then the property will raise the same exceptions as `Widget.query_one`.
67
68
69 Example:
70 ```python
71 from textual import getters
72
73 class MyScreen(screen):
74
75 # Note this is at the class level
76 output_log = getters.query_one("#output", RichLog)
77
78 def compose(self) -> ComposeResult:
79 with containers.Vertical():
80 yield RichLog(id="output")
81
82 def on_mount(self) -> None:
83 self.output_log.write("Screen started")
84 # Equivalent to the following line:
85 # self.query_one("#output", RichLog).write("Screen started")
86 ```
87
88 Args:
89 selector: A TCSS selector, e.g. "#mywidget". Or a widget type, i.e. `Input`.
90 expect_type: The type of the expected widget, e.g. `Input`, if the first argument is a selector.
91
92 """
93
94 selector: str
95 expect_type: type["Widget"]
96
97 @overload
98 def __init__(self, selector: str) -> None:
99 """
100
101 Args:
102 selector: A TCSS selector, e.g. "#mywidget"
103 """
104
105 @overload
106 def __init__(self, selector: type[QueryType]) -> None: ...
107
108 @overload
109 def __init__(self, selector: str, expect_type: type[QueryType]) -> None: ...
110
111 @overload
112 def __init__(
113 self, selector: type[QueryType], expect_type: type[QueryType]
114 ) -> None: ...
115
116 def __init__(
117 self,
118 selector: str | type[QueryType],
119 expect_type: type[QueryType] | None = None,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…