O'Reilly logo

Learning Perl/Tk by Nancy Walsh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required