mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Merge pull request #858 from davep/dom-queries-typo-squish
DOM Queries typo squish
This commit is contained in:
@@ -4,7 +4,7 @@ In the previous chapter we introduced the [DOM](../guide/CSS.md#the-dom) which i
|
||||
|
||||
Selectors are a very useful idea and can do more that 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!
|
||||
|
||||
## Query one
|
||||
## Query one
|
||||
|
||||
The [query_one][textual.dom.DOMNode.query_one] method gets a single widget in an app or other widget. If you call it with a selector it will return the first matching widget.
|
||||
|
||||
@@ -16,7 +16,7 @@ send_button = self.query_one("#send")
|
||||
|
||||
If there is no widget with an ID of `send`, Textual will raise a [NoMatches][textual.css.query.NoMatches] exception. Otherwise it will return the matched widget.
|
||||
|
||||
You can also add a second parameter for the expected type.
|
||||
You can also add a second parameter for the expected type.
|
||||
|
||||
```python
|
||||
send_button = self.query_one("#send", Button)
|
||||
@@ -26,7 +26,7 @@ If the matched widget is *not* a button (i.e. if `isinstance(widget, Button)` eq
|
||||
|
||||
!!! tip
|
||||
|
||||
The second parameter allows type-checkers like MyPy know the exact return type. Without it, MyPy would only know the result of `query_one` is a Widget (the base class).
|
||||
The second parameter allows type-checkers like MyPy to know the exact return type. Without it, MyPy would only know the result of `query_one` is a Widget (the base class).
|
||||
|
||||
## Making queries
|
||||
|
||||
@@ -58,7 +58,7 @@ for button in self.query("Button"):
|
||||
print(button)
|
||||
```
|
||||
|
||||
Any selector that works in CSS will work with the `query` method. For instance, if we want to find all the disabled buttons in a Dialog widget, we could do this:
|
||||
Any selector that works in CSS will work with the `query` method. For instance, if we want to find all the disabled buttons in a Dialog widget, we could do this:
|
||||
|
||||
```python
|
||||
for button in self.query("Dialog Button.disabled"):
|
||||
@@ -116,22 +116,22 @@ If the last widget is *not* a button, Textual will raise a [WrongType][textual.c
|
||||
|
||||
## Filter
|
||||
|
||||
Query objects have a [filter][textual.css.query.DOMQuery.filter] method which further refines a query. This method will return a new query object which widgets that match both the original query _and_ the new selector
|
||||
Query objects have a [filter][textual.css.query.DOMQuery.filter] method which further refines a query. This method will return a new query object with widgets that match both the original query _and_ the new selector
|
||||
|
||||
Let's say we have a query which gets all the buttons in an app, and we want a new query object with just the disabled buttons. We could write something like this:
|
||||
|
||||
```python
|
||||
# Get all the Buttons
|
||||
buttons_query = self.query("Button")
|
||||
buttons_query = self.query("Button")
|
||||
# Buttons with 'disabled' CSS class
|
||||
disabled_buttons = buttons_query.filter(".disabled")
|
||||
disabled_buttons = buttons_query.filter(".disabled")
|
||||
```
|
||||
|
||||
Iterating over `disabled_buttons` will give us all the disabled buttons.
|
||||
|
||||
## Exclude
|
||||
|
||||
Query objects have a [exclude][textual.css.query.DOMQuery.exclude] method which is the logical opposite of [filter][textual.css.query.DOMQuery.filter]. The `exclude` method removes any widgets from the query object which match a selector.
|
||||
Query objects have an [exclude][textual.css.query.DOMQuery.exclude] method which is the logical opposite of [filter][textual.css.query.DOMQuery.filter]. The `exclude` method removes any widgets from the query object which match a selector.
|
||||
|
||||
Here's how we could get all the buttons which *don't* have the `disabled` class set.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user