From 13ace2f80d00b1a97ea58a618c50d4c87de52b61 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Sun, 16 Jun 2024 09:15:40 +0100 Subject: [PATCH] :sparkles: Add an example of screen callbacks working fine For https://github.com/Textualize/textual/issues/4656 --- screen_callback.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 screen_callback.py diff --git a/screen_callback.py b/screen_callback.py new file mode 100644 index 0000000..e72ed01 --- /dev/null +++ b/screen_callback.py @@ -0,0 +1,38 @@ +"""Evidence that screen callbacks are still working. + +For a reply to https://github.com/Textualize/textual/issues/4656. +""" + +from textual import on +from textual.app import App, ComposeResult +from textual.screen import Screen +from textual.widgets import Button + + +class Test(Screen[str]): + def compose(self) -> ComposeResult: + yield Button("Yes", id="Yes") + yield Button("No", id="No") + + @on(Button.Pressed) + def test_the_callback(self, event: Button.Pressed) -> None: + assert event.button.id is not None + self.dismiss(event.button.id) + + +class ScreenCallbackTest(App[None]): + def compose(self) -> ComposeResult: + yield Button("Test") + + @on(Button.Pressed) + def test_screen_callback(self) -> None: + def show_result(result: str) -> None: + self.notify(result) + + self.push_screen(Test(), callback=show_result) + + +if __name__ == "__main__": + ScreenCallbackTest().run() + +### screen_callback.py ends here