How do I force a refresh to take place immediately ? #6135
-
A button click starts a long (a few seconds) computation. I'd like to show some kind of "Please wait" message during the computation, and then reset. @on(Button.Pressed, "#LongComputationButton")
def action_start_long_computation(self):
lbl = self.query_one("#LongComputationLabel")
lbl.content = "Please wait"
do_long_computation()
lbl.content = "Computation finished" The label only refreshes once, after the computation is complete. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
The UI will update when the message handler finishes, which is why you are seeing this. The solution is to make the handler a worker so it runs concurrently. And perform your long computation in a thread so it doesn't stall the event loop. Something like this: @on(Button.Pressed, "#LongComputationButton")
@work
async def action_start_long_computation(self):
lbl = self.query_one("#LongComputationLabel")
lbl.content = "Please wait"
await asyncio.to_thread(do_long_computation)
lbl.content = "Computation finished" |
Beta Was this translation helpful? Give feedback.
-
I'm getting a weird behavior after this change though. After I quit the app, I'm getting the following error: Tcl_AsyncDelete: async handler deleted by the wrong thread
Aborted (core dumped) Any idea what's going on? |
Beta Was this translation helpful? Give feedback.
-
As a matter of fact, I am. Could this be a tkinter issue you have encoutered in the past ? |
Beta Was this translation helpful? Give feedback.
-
After a few tests, the function |
Beta Was this translation helpful? Give feedback.
The UI will update when the message handler finishes, which is why you are seeing this.
The solution is to make the handler a worker so it runs concurrently. And perform your long computation in a thread so it doesn't stall the event loop. Something like this: