Skip to content

Commit 86e9353

Browse files
authored
Merge pull request #5401 from Textualize/query-one-docs
Updates to `query_one` docs in guide
2 parents f57d501 + 83f92f9 commit 86e9353

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

docs/guide/queries.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# DOM Queries
22

3-
In the previous chapter we introduced the [DOM](../guide/CSS.md#the-dom) which is how Textual apps keep track of widgets. We saw how you can apply styles to the DOM with CSS [selectors](./CSS.md#selectors).
3+
In the [CSS chapter](./CSS.md) we introduced the [DOM](../guide/CSS.md#the-dom) which is how Textual apps keep track of widgets. We saw how you can apply styles to the DOM with CSS [selectors](./CSS.md#selectors).
44

55
Selectors are a very useful idea and can do more than apply styles. We can also find widgets in Python code with selectors, and make updates to widgets in a simple expressive way. Let's look at how!
66

@@ -19,7 +19,7 @@ We could do this with the following line of code:
1919
send_button = self.query_one("#send")
2020
```
2121

22-
This will retrieve a widget with an ID of `send`, if there is exactly one.
22+
This will retrieve the first widget discovered with an ID of `send`.
2323
If there are no matching widgets, Textual will raise a [NoMatches][textual.css.query.NoMatches] exception.
2424

2525
You can also add a second parameter for the expected type, which will ensure that you get the type you are expecting.
@@ -41,6 +41,15 @@ For instance, the following would return a `Button` instance (assuming there is
4141
my_button = self.query_one(Button)
4242
```
4343

44+
`query_one` searches the DOM *below* the widget it is called on, so if you call `query_one` on a widget, it will only find widgets that are descendants of that widget.
45+
46+
If you wish to search the entire DOM, you should call `query_one` on the `App` or `Screen` instance.
47+
48+
```python
49+
# Search the entire Screen for a widget with an ID of "send-email"
50+
self.screen.query_one("#send-email")
51+
```
52+
4453
## Making queries
4554

4655
Apps and widgets also have a [query][textual.dom.DOMNode.query] method which finds (or queries) widgets. This method returns a [DOMQuery][textual.css.query.DOMQuery] object which is a list-like container of widgets.

0 commit comments

Comments
 (0)