The Tcl Event Loop
An event loop is built into Tcl. Tcl checks for events and calls out to handlers that have been registered for different types of events. Some of the events are processed internally to Tcl. You can register Tcl commands to be called in response to events. There are also C APIs to event loop, which are described on page 691. Event processing is active all the time in Tk applications. If you do not use Tk, you can start the event loop with the vwait command as shown in Example 16-2 on page 220. The four event classes are handled in the following order:
Window events. These include keystrokes and button clicks. Handlers are set up for these automatically by the Tk widgets, and you can register window event handlers with the bind command described in Chapter 26.
File events. The fileevent command registers handlers for these events.
Timer events. The after command registers commands to occur at specific times.
Idle events. These events are processed when there is nothing else to do. The Tk widgets use idle events to display themselves. The after idle command registers a command to run at the next idle time.