Updating current screen before push_screen
?
#2035
Answered
by
davep
TomJGooding
asked this question in
Q&A
-
Here's a minimal reproduction where a "Login successful!" message should be displayed before moving to the next screen. If you uncomment the I'm guessing this has something to do with concurrency in Textual, but not sure how to approach a possible workaround? Thanks in advance for any advice. import time
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.screen import Screen
from textual.widgets import Input, Label
PASSWORD = "textual"
class LoginScreen(Screen):
def compose(self) -> ComposeResult:
yield Container(
Input(placeholder="Password"),
Label(" "),
)
def on_mount(self) -> None:
self.query_one(Input).focus()
def on_input_submitted(self, event: Input.Submitted) -> None:
password: str = event.input.value.strip()
msg = self.query_one(Label)
if password == PASSWORD:
msg.update("Login successful! Loading...")
# self.app.push_screen("main")
else:
msg.update("Incorrect password!")
class MainScreen(Screen):
def on_mount(self) -> None:
time.sleep(3)
def compose(self) -> ComposeResult:
yield Label("Hello World!")
class ExampleApp(App):
CSS = (
"Screen { align: center middle; }"
"Container { align: center middle; max-width: 100; }"
)
SCREENS = {
"login": LoginScreen,
"main": MainScreen,
}
def on_mount(self) -> None:
self.push_screen("login")
if __name__ == "__main__":
app = ExampleApp()
app.run() |
Beta Was this translation helpful? Give feedback.
Answered by
davep
Mar 13, 2023
Replies: 1 comment 4 replies
-
Rather than just push the screen right away, I'd do this (assuming msg.call_after_refresh(partial(self.app.push_screen,"main")) In other words: once the label has fully updated, then swap to the main screen. |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
TomJGooding
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rather than just push the screen right away, I'd do this (assuming
from functools import partial
):In other words: once the label has fully updated, then swap to the main screen.