Not sure how to "reset" a screen #6217
-
|
Hi everyone. I've got an app with a screen and I would like to be able to force a "reset" while the screen is active; that is, I want to return it to the state it was in when it was first initialized and mounted (including a reset of all input widgets). My question is: what is the best way to accomplish that? My intuition wants to do something like this: from textual.app import App, ComposeResult
from textual.screen import Screen
from textual.widgets import Static, Input, Footer
class LawfulScreen(Screen):
BINDINGS = [("ctrl+s", "quit", "Back to Main"),
("ctrl+backslash", "reset", "Reset")]
def compose(self) -> ComposeResult:
yield Input()
yield Footer()
def action_quit(self) -> None:
self.dismiss()
def action_reset(self) -> None:
self.recompose() # This is not accomplishing what I want
class LawfulApp(App):
BINDINGS = [("n", "new", "New Screen")]
SCREENS = {"test": LawfulScreen}
def compose(self) -> ComposeResult:
yield Static("Some filler text")
yield Footer()
def action_new(self) -> None:
self.push_screen("test")
if __name__ == "__main__":
app = LawfulApp()
app.run()I used However, here is something that does what I want: from textual.app import App, ComposeResult
from textual.screen import Screen
from textual.widgets import Static, Input, Footer
class ChaoticScreen(Screen[bool]):
BINDINGS = [("ctrl+s", "quit", "Back to Main"),
("ctrl+backslash", "reset", "Reset")]
def compose(self) -> ComposeResult:
yield Input()
yield Footer()
def action_quit(self) -> None:
self.dismiss(False)
def action_reset(self) -> None:
self.dismiss(True)
class ChaoticApp(App):
BINDINGS = [("n", "new", "New Screen")]
def compose(self) -> ComposeResult:
yield Static("Some filler text")
yield Footer()
def action_new(self) -> None:
def on_dismissal(quit) -> None:
if quit:
self.action_new()
self.push_screen(ChaoticScreen(), on_dismissal)
if __name__ == "__main__":
app = ChaoticApp()
app.run()I'm willing to go with this, but it feels a little bit cursed to me. My issue with it is that the "ctrl+backslash" action doesn't work on every instance of the screen; just the ones that are instantiated with "ctrl+n". Obviously I could just define I'd appreciate any advice. I'm pretty new to this and don't really know what I'm doing yet. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Recompose is a coroutine. You will need to await it for it to work. Or you can call refresh(recompose=True) which will recompose after any pending events have been handled. |
Beta Was this translation helpful? Give feedback.
Recompose is a coroutine. You will need to await it for it to work. Or you can call refresh(recompose=True) which will recompose after any pending events have been handled.