I see some usage of after 0 <command>. I wonder what is the point here, why not directly call the command? Another words, what is the difference here:
after 0 "puts hi"
puts hi
Thanks.
What the after 0 … does is effectively put the supplied command call at the front of the queue of activity to be processed by the event loop, except it is behind all currently scheduled activity (e.g., because its socket is already readable or its timer event has already triggered). This effectively delays the running of the command for as short as possible without starving other event handlers.
When you're running interactively in a situation that is running the event loop (such as within wish or tkcon), there's usually very little difference.
The other form to be aware of is after idle, which delays the execution of the command until the code would otherwise have to enter a blocking wait for an event. This is used heavily in Tk to do graphical update coalescing, since it is common for many different events to be delivered at once, e.g., changing the size, focus-status, mouse position, and repaint-required nature of a window at once. Pure Tcl scripts don't need this feature nearly so much.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With