Anatomy of a Tk program

You can download the code samples here.

If you think about the GUI programs you often use, you probably have some strong reasons for liking them (and probably some even stronger reasons for not using some others). Although everyone will have different priorities and preferences, probably most will agree that a well-thought out GUI should

Consider, for example, the WS_FTP client program (available for Windows) - a screen shot of the main window of this program appears below.

Figure 2.1: Main window of the WS_FTP ftp client
Image wsftp

After perusal of this interface for a few moments, people familiar with ftp clients will probably easily see how to do the most common operations - connect to a remote host, close the connection, navigate through a directory tree, create or delete directories, get, put, or delete files, and quit the program. This window also illustrates most of the widgets we will encounter in the next Chapter - listboxes for file listings, radio buttons for specifying the file transfer mode, a label area where messages about the current operation are logged, and various buttons to invoke commands. Many of these buttons, when clicked, will furthermore bring up dialog boxes to specify further information - for example, the ChgDir button will bring up another window by which the user can type in the name of the directory she/he wishes to go to. Finally, although it's not visible in this screen shot, clicking with the right mouse button in an area will bring up a menu with some common commands the user can choose from.

In this Chapter we will explore the basics of a Perl/Tk program. We first discuss creating the main window, or in general a top level window. The ``meat'' of a Tk program is the widgets contained within these windows - we discuss the specifics of various windows in the next Chapter, but in this Chapter we will discuss how to place widgets within a window. We also discuss the creation of frames, which allows for a greater control over widget placement. Finally, we introduce the topic of events as related to Tk programs, particularly the actions taken through callbacks and also the use of binding between event sequences and widgets.

Randy Kobes 2003-11-17