Chapter 37. Events

Steve Lidie

event (î-vênt’): something that happens: a noteworthy occurrence or happening: something worthy of remark: an unusual or significant development. (Paraphrased from Webster’s Third.)

Events are what drive Perl/Tk programs. In the past I’ve described these events superficially, sweeping lots of detail under the MainLoop rug, all for the sake of simplicity. MainLoop is our friend, since it’s all that is needed for nearly every Perl/Tk program. But sometimes it’s not enough.

Today’s featured program is a simple Pong-like game sporting a new widget derived from the Canvas class, which we’ll compare to the Odometer composite widget described in The Mouse Odometer. Instead of using MainLoop, our Pong game handles events itself with DoOneEvent.

Before discussing Pong, we’ll examine some other programs, including a simple animation called neko, demonstrating the Photo widget and some other Tk commands.

Tk defines four broad event categories: X, timer, input/output, and idle. X events are generated in response to mouse motion, button and keyboard actions, and window changes. You already know that many of these events have built-in Tk bindings, and that you can create your own bindings, so all you need to do is define the callback to handle the event. (There are lots of other X events, which we’ll examine in detail in subsequent articles.) Timer events are used for periodic occurrences, from blinking items to animating images. Input/output events help prevent your ...

Get Web, Graphics & Perl/Tk Programming 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.