Async mechanics in App vs Screen event handlers #5561
-
I have a test App, with a Screen that contains a button ('screenButton') and static widget ('screenWidget'), and the following button-press handler that's supposed to update the static widget twice -- once immediately, and another time after a delay:
When I place this handler in the app class, it works as expected -- the static widget shows 'State 1' immediately after button press, and then after a 2s delay, 'State 2' is displayed. However (and this was the behavior that originally stumped me), when this handler is placed in the Screen class definition, the code stops working as expected -- 'State 1' is never shown, and then after 2s 'State 2' appears. Basically, asyncio.sleep() doesnt seem to work, it blocks (?), so you never see 'State 1' before 'State 2'. So my main question is -- what are the ramifications of this behavior, in terms of structuring textual apps when working with async stuff? Does it mean that one can't do certain (async or otherwise) event handling in a Screen class (how about custom Widgets?) Or is it due to something simple that I'm missing, e.g. some particulars of the asyncio.sleep() function? Thank you! p.s. My overall context is that I'd like to create a simple rpg game with multiple "screens" or "scenes" (e.g. "MainMenu", "CharacterBuilder", "GameScreen", etc), where some processes might run asynchronously, and I'm trying to figure out if Screens are an appropriate way to structure things, as opposed to tabbed content or content switching, or something else... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The app won't refresh until the handler completes. You have two options: |
Beta Was this translation helpful? Give feedback.
The app won't refresh until the handler completes.
You have two options:
@work