In App class, self.tree does not contain all screens #2295
-
As the title says, when using Here is a minimal example:
This will show the App's tree before and after
Textual Diagnose: Textual DiagnosticsVersions
Python
Operating System
Terminal
Rich Console options
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
This would make sense. Once you push the quit screen it is the application's screen, and so the app's tree will be composed of that screen. The exception you get here from This feels like the sort of time where you don't want to be relying on using the default screen, and don't want to be doing the main work in the app; but instead should create a main screen that you're in control of and do the update work in there. For example: from datetime import datetime
from textual.app import App, ComposeResult
from textual.screen import ModalScreen, Screen
from textual.widgets import Static, Button, Label
from textual.containers import Grid
class QuitScreen(ModalScreen):
"""Screen with a dialog to quit."""
def compose(self) -> ComposeResult:
yield Grid(
Label("Are you sure you would like to quit?", id="question"),
Button("Quit", variant="error", id="quit"),
Button("Cancel", variant="primary", id="cancel"),
id="dialog",
)
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "quit":
self.app.exit()
else:
self.app.pop_screen()
class ClockScreen(Screen):
def compose(self) -> ComposeResult:
yield Static(str(datetime.now()), id="time")
def on_mount(self) -> None:
self.set_interval(1, self.update_loop)
def update_loop(self) -> None:
self.query_one("#time", Static).update(str(datetime.now()))
class Clock(App):
BINDINGS = [("x", "request_exit", "Exit Clock")]
def on_mount(self):
self.push_screen(ClockScreen())
def action_request_exit(self) -> None:
self.push_screen(QuitScreen())
if __name__ == "__main__":
app = Clock()
app.run() |
Beta Was this translation helpful? Give feedback.
-
This is intentional. There is only every one screen in the DOM (the current screen), for the following reasons:
If you want to get a widget from a specific screen you could pass a reference to the screen instance, or get it via the app with get_screen. |
Beta Was this translation helpful? Give feedback.
This is intentional. There is only every one screen in the DOM (the current screen), for the following reasons:
If you want to get a widget from a specific screen you could pass a reference to the screen instance, or get it via the app with get_screen.