Closed gsportelli closed 2 months ago
However, onece in a while it exits abruptly with an IvalidStateError
I wonder perhaps this only occurs when you trigger action_info
when you're already on the InfoScreen
?
I don't think so, but using ModalScreen instead of Screen seems to solve the problem. I couldn't test it thoroughly though. EDIT: To make it happen, I open and close the dialog quickly several times, so maybe when the screen is not modal it might happen that action_info is called when the previous screen is not fully dismissed as you say.
Here's a quick MRE which I think demonstrates the issue:
InvalidStateError
from textual import on, work
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.screen import Screen
from textual.widgets import Button, Footer, Label
class InfoScreen(Screen[bool]):
def __init__(self, question: str) -> None:
self.question = question
super().__init__()
def compose(self) -> ComposeResult:
yield Vertical(
Label(self.question, id="info-label"),
Button("Ok", variant="primary", id="ok"),
id="info-vertical",
)
yield Footer()
@on(Button.Pressed, "#ok")
def handle_ok(self) -> None:
self.dismiss(True) # Changed the `dismiss` result to compatible type
class ExampleApp(App):
BINDINGS = [("i", "info", "Info")]
screen_count = 0
def compose(self) -> ComposeResult:
yield Label("This is the default screen")
yield Footer()
@work(exclusive=True)
async def action_info(self) -> None:
self.screen_count += 1
await self.push_screen_wait(
InfoScreen(f"This is info screen #{self.screen_count}")
)
if __name__ == "__main__":
app = ExampleApp()
app.run()
using ModalScreen instead of Screen seems to solve the problem.
That's because the ModalScreen
will temporarily disable the main interface, so pressing i in my example above wouldn't have any effect.
Don't forget to star the repository!
Follow @textualizeio for Textual updates.
Please give a brief but clear explanation of the issue. If you can, include a complete working example that demonstrates the bug. Check it can run without modifications.
I'm creating a dialog box as follows:
Then I call the dialog as follows:
When I do so, the dialog opens and I can click on the button to dismiss it. However, onece in a while it exits abruptly with an IvalidStateError
It will be helpful if you run the following command and paste the results:
Textual dignose does not work. Here are the Python and textual versions:
Feel free to add screenshots and / or videos. These can be very helpful!