| Previous | Next
Perl/TkContents:Widgets
Perl/Tk is an extension for writing Perl programs with a graphical user interface (GUI) on both Unix and Windows/NT. Tk was originally developed as an extension to the Tcl language, for use with the X Window System on Unix. With its port to Perl, Tk gives Perl developers the same control over the graphical desktop that Tcl developers have taken for granted. The Tk extension makes it easy to draw a window, put widgets into it (such as buttons, checkboxes, entry fields, menus, etc.), and have them perform certain actions based on user input. A simple "Hello World" program would look like this:
When you run it, it would look like Figure 21-1. Figure 21-1. A simple Perl/Tk programClicking on the Hello World button exits the program, and your window disappears. Let's walk through these few lines of code. After calling the Perl interpreter, the program calls the Tk module. It then proceeds to build a generic, standard window ( The last line tells the program to "go do it." WidgetsWidgets in Perl/Tk are created with widget creation commands, which include Positioning widgets is done with geometry managers. In the "Hello World" example shown earlier, the Widget MethodsWidgets can be configured, queried, or manipulated via various widget methods. For example, all widgets support the Widget methods are listed in the discussion of each widget later in this chapter. However, since all widgets support the The configure methodThe $button->configure(-width => 100); To get the value for a current widget, just supply it without a value: $button->configure(-width); The result is an array of scalars; the important values are the last two, which represent the default value and its current value, respectively. You can also call The cget methodFor simply retrieving the value of an option, ScrollbarsMany widgets have scrollbars associated with them. Scrollbars can be added to a widget in two ways: with an independent Scrollbar widget or with the Using the Scrolled methodUse the
This creates an Entry widget with an "optional" scrollbar on the bottom. The first argument to Any additional options to the The Scrollbar widgetFor more flexibility with a scrollbar, you can use the Scrollbar widget. To do so, you need to create the target widget to scroll. Set the $scrollbar = $mainwindow->Scrollbar(-orient => 'vertical'); $listbox = $mainwindow->Entry(-yscrollcommand => ['set' => $scrollbar]); $scrollbar->configure(-command => ['yview' => $listbox]); $scrollbar->pack(-side => 'right', -fill => 'y'); $listbox->pack(-side => 'left', -fill => 'both'); First, we create the scrollbar with vertical orientation (which is actually the default). Next, we create the Listbox widget with the CallbacksMany widgets allow you to define a callback, which is a command to execute when the widget is selected. For example, when you press an exit button, the callback might be to a routine that cleans up and quits the program. When you click on a radio button, you might want to change the window to reflect the new preferences. Widgets that support callbacks have a Colors and FontsTk was originally created for the X Window System and is still primarily used in that environment. For this reason, it has inherited the font and color scheme used for the X Window System. Colors that can be used with Tk widgets are identified either by an RGB value or by a name that has been associated with an RGB value. In general, it is easier to use a color name than an explicit RGB value. For a listing of the color names that are supported, see the rgb.txt file in your X distribution or use the showrgb command. (Most common color names are supported, so you can say things such as "red," "pink," "green," and even "chartreuse" with confidence.) Fonts are another matter. Under the X Window System, fonts are named things such as -adobe-helvetica-medium-o-normal -- 12-120-75-75-p-67-iso8859-1. Wildcards can make the fonts easier to use, but they're still a mouthful. For a listing of fonts available for a particular X server, use the xlsfonts command. There are a few font "aliases" that have been defined for your convenience (such as |