Programming the X Window System is like trying to find the square root of pi using Roman numerals.
In this chapter, we learn how to build graphical user interfaces (GUIs) using one of the most feature-rich and professional-looking toolkits around: the Tk toolkit [ 4 ]. We start with a brief tour of most of the Tk widgets as well some of the Tix extension and then learn about geometry management (how to arrange widgets on a form). Next, we briefly examine Perl's support for timers, used extensively in Chapter 15, GUI Example: Tetris . We then cover event bindings, which allow us to map arbitrary combinations of mouse and keyboard events to callbacks. Finally, we deal with event loop issues similar to the ones we studied in Chapter 12, Networking with Sockets .
For the sake of simplicity, this chapter presents small snippets of code to illustrate the widgets and other Tk features in relative isolation; we will rely on the next two chapters to cut a broad swath through all these aspects and to apply them to practical problems.
While we are on the subject of building user interfaces, please do yourself (and your users) a favor by reading Alan Cooper's[ 1 ] excellent and refreshingly opinionated book, About Face: The Essentials of User Interface Design [ 6 ].
At the most basic level, all windowing platforms (Apple Macintosh, X Windows, and Microsoft Windows) are very simple. They provide a low-level API to create and manage windows, to report interesting events such as mouse and keyboard events, and to draw graphical elements such as lines, circles, and bitmaps. The problem is that drawing even a simple form takes a considerable amount of code and reading thousands of pages of documentation (literally).
Often-used patterns of GUI code have evolved into widgets (called "controls" in the Microsoft Windows world); examples include buttons, scrollbars, and listboxes. Building a GUI is now a simple matter of launching an interactive form designer and dragging and dropping these ready-made components into a layout of your choice. Object-oriented programming has never been easier.
It turns out that widgets and scripting languages are a perfect match. Widgets have simple interfaces, and form-based GUIs are not performance-critical. Both of these attributes make GUIs a very fertile ground for scripting. Combine that with the fact that GUIs need the most configurability (because that's the part of the application the user deals with, and for the most part, the GUI is the application), and you can understand the enormous popularity of tools such as Visual Basic, PowerBuilder, and Hypercard.
On Unix systems, X Windows has been the windowing platform of choice. Several widget toolkits have been built over X: Athena, InterViews, Motif, and Tk. For professional good looks, ease of use, and documentation, you can't beat Tk. To top it all, it is free!
Unlike other widget toolkits, Tk was developed expressly to be driven by a scripting language: Tcl.[ 2 ] Indeed, it can be argued that Tk is the chief reason for Tcl's popularity. There are a lot of people who don't like Tcl as a scripting language but love Tk, and have attempted to adapt it to their favorite scripting language - Scheme, Python, Guile, and, of course, Perl. Malcolm Beattie made the initial attempt to provide a Perl layer that internally used the Tcl interpreter to reach the Tk library.
Nick Ing-Simmons tried a more ambitious approach: he purged Tk of all embedded Tcl code and gave it a generic porting layer to make it easy to add other scripting languages; this effort is called pTk (portable Tk). To this, he added a Perl5 wrapper (with the intention of adding other language wrappers in the future). This combination of pTk and the Perl wrapper module Tk.pm, referred to as Perl/Tk, is the subject of this chapter.
Meanwhile, Dr. Ousterhout's team at Sun ported both Tcl and Tk to Microsoft Windows and the Mac, and the Perl/Tk combination has followed suit fairly closely. Other portable GUI options are Tcl/Tk, of course, and Python/Tk (which does not rely on pTk). Microsoft is porting its ActiveX (formerly OLE) and VBA (Visual Basic for Applications) to the Unix environment, so they might be formidable competition soon. The VB toolkit itself doesn't come anywhere close to Perl and Tk's functionality, but the development environment and third-party support is unbeatable. We live in interesting times!
Several new professional-looking widgets have been added to Tk in the form of an extension library called Tix, developed by Ioi Kim Lam. These include balloons (for help messages), notebooks, and spreadsheet-like grid widgets. The good news is that the Perl/Tk distribution contains a Perl layer over this effort also.