-
So i am playing around with textual but i couldn't figure out how to push a screen from a screen I am doing this in my custom screen class when pushing a button
When i press the button in ICMPPingScreen, i don't see the new ResultScreen that suppose to appear, sometimes it appears for a split second and disappears, result is the same if i change ResultScreen to Screen class from ModalScreenh class. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
This seems to work as expected: from textual.app import App, ComposeResult
from textual.screen import ModalScreen, Screen
from textual.widgets import Button, Static
class ResultScreen(ModalScreen):
"""This is a modal screen that holds result information"""
BINDINGS = [
("escape", "app.pop_screen", "Exit"),
]
def compose(self) -> ComposeResult:
yield Static("addr", id="addr")
yield Static("min_rtt", id="min_rtt")
yield Static("avg_rtt", id="avg_rtt")
yield Static("max_rtt", id="max_rtt")
class ICMPPingScreen(Screen):
BINDINGS = [
("escape", "app.pop_screen", "Exit"),
]
def compose(self) -> ComposeResult:
yield Button("Ping", id="pi")
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "pi":
self.app.push_screen(ResultScreen())
class ExampleApp(App):
def on_mount(self) -> None:
self.push_screen(ICMPPingScreen())
if __name__ == "__main__":
app = ExampleApp()
app.run() |
Beta Was this translation helpful? Give feedback.
-
This is my full code from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, Button, Static
from textual.containers import ScrollableContainer
from textual.reactive import reactive
from textual.screen import ModalScreen, Screen
class ResultScreen(ModalScreen):
"""This is a modal screen that holds result information"""
BINDINGS =[
("escape", "app.pop_screen", "Exit"),
]
def compose(self) -> ComposeResult:
yield Static("addr",id="addr")
yield Static("min_rtt",id="min_rtt")
yield Static("avg_rtt",id="avg_rtt")
yield Static("max_rtt",id="max_rtt")
class ICMPPingScreen(Screen):
BINDINGS =[
("escape", "app.pop_screen", "Exit"),
]
def compose(self) -> ComposeResult:
yield Button("Ping", id="pi")
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "pi":
self.app.push_screen(ResultScreen())
class PQPingApp(App):
"""This is the main app for ping utility"""
BINDINGS =[
("d", "toggle_dark", "Toggle dark mode"),
]
def compose(self) -> ComposeResult:
yield Header()
yield Footer()
yield Button("Ping", id="pi")
def on_mount(self) -> None:
self.install_screen(ICMPPingScreen(), name="ICMPPing")
self.install_screen(ResultScreen(), name="ICMPResult")
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "pi":
self.push_screen(ICMPPingScreen())
if __name__ == "__main__":
app = PQPingApp()
app.run() |
Beta Was this translation helpful? Give feedback.
-
Okay so problem was, both button in app and button in ICMPPingScreen had the same id, when i press the button in the ICMPPingScreen it was invoking the function in ICMPPingScreen first then later function in app Solution was to simply give them different ids. |
Beta Was this translation helpful? Give feedback.
Okay so problem was, both button in app and button in ICMPPingScreen had the same id, when i press the button in the ICMPPingScreen it was invoking the function in ICMPPingScreen first then later function in app
Solution was to simply give them different ids.