1.9. The Anatomy of an Event Loop

When you are programming an application that uses a graphical interface rather than a textual interface, there are a lot of different things to consider. In a text-based application, you can read from standard input (STDIN), use command-line options, read files, or prompt the user for specific information. The keyboard is your only avenue of input from the user. In a GUI, input can not only come from those places, but it can also come from the mouse and the window manager (such as a "close" directive from a window manager like mwm or MS Windows). Although this extra input allows more flexibility in our applications, it also makes our job more difficult. As long as we tell it what to do, Perl/Tk helps us handle all that extra input gracefully.

Input in a GUI is defined by events. Events are typically different combinations of using the keyboard and mouse at the same or different times. If the user pushes the left mouse button on button "B", that is one type of event. Pushing the right mouse button on button "C" would be another event. Typing the letter "a" would be another event. Yet another event would be holding down the Control key and clicking with the middle mouse button. You get the idea.

Events are processed during an event loop. This event loop does just what its name says—it handles events during a loop. It determines what subroutines to call based on what type of event happened. Here is a pseudo-code event loop:

while (1) { get_event_info ...

Get Learning Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.