Updating TextInput on switch/button on_toggle & off_toggle #1518
-
How do I implement TextInput update multiple times? Ex.
This one is only getting updated at the end instead of at each assignment. Also, how do I update TextInput when it's off toggle? I haven't found any attribute similar to off_toggle. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The update behavior you're seeing needs a little explanation, but it comes down to "When are you giving the GUI toolkit an opportunity to update itself?" You're not - and as a result, the widget isn't updating. You may have noticed that you start an app by invoking a So - in your case, you're pressing a switch, which is invoking a handler, the handler is modifying an output... but you're never giving control back to the app to run the event loop, and as a result, the app never gets an opportunity to redraw to reflect that change. When the handler finishes, the app can now redraw, and it reflects the current state of the app - the final value that you assigned. You may have notice apps that "beachball" or throw up a "non-responsive" message of some kind. That's essentially the same thing that is happening here - the operating system is telling you that your app hasn't called the redraw function for a while. This usually happens when some form of IO (e.g., network or disk access) takes longer than the programmer expected, and the app locks up waiting for that IO to complex. If you increase the number of loops in your handler, to 100, I'd wager you will see your app "beachball", because the operating system will notice that it hasn't had a chance to update the GUI for a while. This isn't a Toga specific thing either - essentially every GUI toolkit works in a very similar way. The fix is to release control to the app on a regular basis so it has a chance to redraw. You've even got a convenient point to do that release - you're sleeping for 2 seconds. There's no reason your handler needs to retain control of the app for those 2 seconds - so you can tell Toga that you'd like to release control to the app for 2 seconds, and resume once that is done. Toga has 2 ways you can do this. Firstly, you can modify the handler to be a generator; any value yielded by the generator will be interpreted as the duration that you'd like to release control:
The other approach is to make the handler an async co-routine, and
As a general rule, you need to ensure that any code you write, especially in handlers, will complete as fast as possible, so that your app doesn't lock up. That means breaking your logic into smaller pieces, providing regular release opportunities for the app to redraw. As for the second question - "on_toggle" doesn't mean "when toggled on", it means "on a toggle event". You should get a toggle with both the on and off change; you can use the current |
Beta Was this translation helpful? Give feedback.
The update behavior you're seeing needs a little explanation, but it comes down to "When are you giving the GUI toolkit an opportunity to update itself?" You're not - and as a result, the widget isn't updating.
You may have noticed that you start an app by invoking a
main_loop()
method - that main loop is the event loop. It's essentially a tight loop that say "while App Running: redraw app" (it's a little more complex than that in practice, but it's close enough for this explanation). When you're not interacting with it, the app is constantly redrawing in the background; whenever an event occurs (like a button click), your handler is queued into the event loop for execution. However, the …