Returning integer data from a modal screen. #5388
-
Sorry for what is probably a real n00b question - but can anyone show me an example of how you can return string or integer data from a modal screen? All the examples i've seen typically show two buttons and then simply return a bool or True or False. In my case i've created a modal screen where a user can input a numeric value and i'd like to return it to the caller when they either hit return or click the OK button. I'd be really grateful if someone could post a working example of this. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Here's a quick recap of how to return data from screens from the docs:
For example: from textual.app import App, ComposeResult
from textual.screen import ModalScreen
from textual.validation import Integer
from textual.widgets import Input
class ExampleScreen(ModalScreen[int]):
def compose(self) -> ComposeResult:
yield Input(validators=Integer())
def on_input_submitted(self, event: Input.Submitted) -> None:
if event.validation_result.is_valid: # type: ignore[union-attr]
self.dismiss(int(event.value))
class ExampleApp(App):
def on_mount(self) -> None:
def notify_input(value: int | None) -> None:
self.notify(str(value))
self.push_screen(ExampleScreen(), notify_input)
if __name__ == "__main__":
app = ExampleApp()
app.run() |
Beta Was this translation helpful? Give feedback.
Here's a quick recap of how to return data from screens from the docs:
For example: