-
|
After installing a newer version of textual, I notice a different behavior when mounting widgets to other widgets. In the code provided below when the button is pressed I get NoMatches error, although import textual
from textual.app import App, ComposeResult
from textual.containers import Container, ScrollableContainer
from textual.widgets import Static, Button, ContentSwitcher
class Results(Static):
def compose(self) -> ComposeResult:
yield Static("Results")
class Test(App):
def compose(self) -> ComposeResult:
yield Results(id="results-widget")
yield Button("Press")
def on_button_pressed(self, event: Button.Pressed) -> None:
self.query_one('#results-widget').mount(ContentSwitcher(Container(id="lines"), ScrollableContainer(Static(id="plain"), id="plain-container"), initial="lines", id='switcher'))
item = self.query_one('#lines')
if __name__ == "__main__":
app = Test()
app.run()However, the below code works: import textual
from textual.app import App, ComposeResult
from textual.containers import Container, ScrollableContainer
from textual.widgets import Static, Button, ContentSwitcher
class Results(Static):
def compose(self) -> ComposeResult:
yield Static("Results")
yield ContentSwitcher(Container(id="lines"), ScrollableContainer(Static(id="plain"), id="plain-container"), initial="lines", id='switcher')
class Test(App):
def compose(self) -> ComposeResult:
yield Results(id="results-widget")
yield Button("Press")
def on_button_pressed(self, event: Button.Pressed) -> None:
item = self.query_one('#lines')
if __name__ == "__main__":
app = Test()
app.run()I would expect both pieces of code to have the exact same behavior, as was the case a few versions of textual before (0.42.0 or 0.45.0 if I remember correctly) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
In your first example, you'd need to --- vpet98.py-orig 2024-02-05 12:01:59
+++ vpet98.py 2024-02-05 12:02:05
@@ -14,8 +14,8 @@
yield Results(id="results-widget")
yield Button("Press")
- def on_button_pressed(self, event: Button.Pressed) -> None:
- self.query_one('#results-widget').mount(ContentSwitcher(Container(id="lines"), ScrollableContainer(Static(id="plain"), id="plain-container"), initial="lines", id='switcher'))
+ async def on_button_pressed(self, event: Button.Pressed) -> None:
+ await self.query_one('#results-widget').mount(ContentSwitcher(Container(id="lines"), ScrollableContainer(Static(id="plain"), id="plain-container"), initial="lines", id='switcher'))
item = self.query_one('#lines')
if __name__ == "__main__": |
Beta Was this translation helpful? Give feedback.
In your first example, you'd need to
awaitthemountbefore you could query back the widgets: