Chapter 33. A Perl/Tk Roadmap

Jon Orwant

While editing TPJ, I often had trouble finding authors to write good beginner articles. I knew plenty of experts, but they usually preferred articles that displayed their expertise. Paradoxically, good articles on simple topics can be hard for experts to write, because it’s been a long time since they were beginners and they may not remember all of the pitfalls they encountered when they started out.

Steve Lidie, the author of most of the articles in this section, doesn’t have that problem: he’s written for both beginners and experts. As I write this, he just finished Mastering Perl/Tk for O’Reilly (co-authored with Nancy Walsh), which adapted five of his ten TPJ articles that were originally planned for this section. We didn’t feel right having similar Perl/Tk material appear in two of our books, so now you’ll have to settle for me.

I’m an intermediate Perl/Tk programmer. Every so often I need to create an interactive graphical application, which I always find I can slap together quickly with Perl/Tk. But I do it infrequently enough that I forget the names of all the widgets and functions, and the order of their parameters. This makes me an unimpressive Perl/Tk programmer, but it has a hidden silver lining—the lack of familiarity with the material allows me to help novices learning Perl/Tk for the first time.

The goal of this article is modest: I’m going to give a nearly code-free roadmap to the Perl/Tk universe, explain what I think is important and what isn’t, and then step out of the way so that Steve, the expert, can show you how it’s done in subsequent articles.

Understand the Basics

Perl/Tk applications are Perl programs that use the Tk module. There are three basic concepts in Perl/Tk programming that distinguish it from regular Perl programming.

  1. Perl/Tk is all about widgets. (“Widget” is a name used for something when you can’t think of anything else to call it.) In Perl/Tk, widgets are graphical elements like buttons, scrollbars, and menus.

  2. Perl/Tk is event-driven. Most of your programs will have a MainLoop statement in them; when Perl executes it, your application will go into stasis, taking action only in response to user-created events such as mouse clicks or key presses.

  3. Perl/Tk makes extensive use of callbacks. For instance, when you create a button, you have to tell Perl/Tk what Perl code to execute when the user presses that button. The following expression creates a callback that prints You pressed a button.

    $mw->Button(  -text => "Press me!",
                -command => sub { print "You pressed a button." }
               );

Ignore What You Don’t Need

Perl/Tk is huge, and you shouldn’t care. Chances are you only need a tiny subset to create your application. By my count, there are 39 basic widgets, and I almost always get by with just nine (Button, Canvas, Dialog, Frame, Label, MainWindow, Menu, Text, and TopLevel).

Some other Perl/Tk features I’ve never needed:

  • The ability to create my own Perl/Tk widgets

  • Perl/Tk’s option database

  • Tix widgets

  • Interprocess communication

  • Using C from Perl/Tk

If any of those topics interest you, or if you simply want the definitive Perl/Tk book, Mastering Perl/Tk is a natural next step.

If you’re determined to create applications without understanding what you’re doing, you can still get remarkably far by examining some of Steve’s programs (on both http://www.oreilly.com/catalog/mastperltk/ and http://www.oreilly.com/catalog/tpj2/) and the example programs bundled with the Perl/Tk distribution, finding one that approximates the behavior you need, and chopping out the code for behaviors you don’t.

To help you decide what you don’t need, Table 33-1 lists all the basic widgets, what they do, and what options they accept.

Table 33-1. Perl/Tk widgets

Widget

Purpose

Options

Adjuster

Gives user control of widget size

Same as Frame plus -restore, -side, -delay, -widget

Balloon

Displays text when mouse is positioned over a widget

Same as MainWindow plus -font, -balloonposition, -statusmsg, -postcommand, -installcolormap, -initwait, -state, -cancelcommand, -balloonmsg, -motioncommand, -statusbar

Bitmap

Simple collection of pixels

-background, -data, -file, -foreground, -maskdata, -maskfile

BrowseEntry

Drop-down listbox that lets user type entries

Same as Entry plus -labelHighlightcolor, -labelHeight, -labelOffset, -arrowimage,

-label, -labelWraplength, -labelRelief, -labelPadx, -labelPady, -labelTextvariable,

-labelJustify, -command, -labelHighlightbackground, -options, -labelFont,

-labelVariable, -labelBackground, -listcmd, -labelImage, -labelTile, -labelActivetile,

-listwidth, -variable, -labelCursor, -browsecmd, -labelTakefocus, -labelDisabledtile,

-labelAnchor, -labelHighlightthickness, -labelText, -labelWidth, -colorstate, -labelPack, -labelForeground, -choices, -labelUnderline, -labelBitmap, -labelBorderwidth

Button

Something the user presses to trigger an action

Same as Label plus -default, -state, -command, -disabledforeground,

-activeforeground, -activeimage, -activebackground

Canvas

A 2D area giving you pixel-by-pixel control

-activegroup, -background, -bd, -bg, -borderwidth, -closeenough, -confine, -cursor,

-disabledtile, -height, -highlightbackground, -highlightcolor, -highlightthickness,

-insertbackground, -insertborderwidth, -insertofftime, -insertontime, -insertwidth,

-offset, -relief, -scrollregion, -selectbackground, -selectborderwidth,

-selectforeground, -state, -takefocus, -tile, -width, -xscrollcommand,

-xscrollincrement, -yscrollcommand, -yscrollincrement

Checkbutton

A Button that the user can select or deselect

Same as Label plus -onvalue, -selectcolor, -variable, -offvalue, -state, -command,

-disabledforeground, -activeforeground, -indicatoron, -selectimage,

-activebackground

ColorEditor

Lets user select colors

-background, -bd, -bg, -borderwidth, -class, -color, -color_space, -colormap,

-command, -container, -cursor, -display_status, -fg, -foreground, -height, -highlight, -highlightbackground, -highlightcolor, -highlightthickness, -initialcolor, -offset,

-overanchor, -popanchor, -popover, -relief, -takefocus, -tile, -title, -visual,

-widgets, -width

Dialog

Pop-up window for immediate user action

Same as Label plus -title, -overanchor, -command, -popover, -popanchor

DirTree

HList tailored for

showing directories

Same as HList plus -dircmd, -value, -image, -showhidden, -opencmd, -ignoreinvoke,

-closecmd, -directory

Entry

Lets users enter a little text

-background, -bd, -bg, -borderwidth, -cursor, -disabledtile, -exportselection, -fg,

-font, -foreground, -fgtile, -foregroundtile, -highlightbackground, -highlightcolor,

-highlightthickness, -insertbackground, -insertborderwidth, -insertofftime,

-insertontime, -insertwidth, -invalidcommand, -invcmd, -justify, -offset, -relief,

-selectbackground, -selectborderwidth, -selectforeground, -show, -state,

-takefocus, -textvariable, -tile, -validate, -validatecommand, -vcmd, -width,

-xscrollcommand

ErrorDialog

Dialog for alerting user about application errors

-appendtraceback, -background, -bd, -bg, -borderwidth, -class, -cleanupcode,

-colormap, -container, -cursor, -fg, -foreground, -height, -highlightbackground,

-highlightcolor, -highlightthickness, -menu, -offset, -relief, -screen, -takefocus, -tile,

-use, -visual, -width

FileSelect

Listbox tailored for files

Same as Listbox plus -labelHighlightcolor, -resetlabel, -labelHeight, -labelOffset,

-scrollbars, -homelabel, -verify, -defaultextension, -label, -labelWraplength,

-labelRelief, -labelPadx, -labelPady, -labelTextvariable, -labelJustify, -command,

-labelHighlightbackground, -filelabel, -labelFont, -labelVariable, -initialdir,

-labelBackground, -create, -title, -labelImage, -transient, -labelTile, -labelActivetile,

-cancellabel, -filter, -labelCursor, -acceptlabel, -labelTakefocus, -overanchor,

-labelDisabledtile, -dirlabel, -labelAnchor, -labelHighlightthickness, -labelText,

-labelWidth, -regexp, -dirlistlabel, -labelPack, -initialfile, -accept, -directory,

-labelForeground, -popover, -popanchor, -labelUnderline, -labelBitmap,

-labelBorderwidth, -filelistlabel

Frame

Container for other widgets

-background, -bd, -bg, -borderwidth, -class, -colormap, -container, -cursor, -fg,

-foreground, -height, -highlightbackground, -highlightcolor, -highlightthickness,

-label, -labelPack, -labelVariable, -offset, -relief, -takefocus, -tile, -visual, -width

HList

Hierarchical list

-background, -bd, -bg, -borderwidth, -browsecmd, -columns, -command, -cursor,

-dragcmd, -drawbranch, -dropcmd, -exportselection, -fg, -font, -foreground, -gap,

-header, -height, -highlightbackground, -highlightcolor, -highlightthickness, -indent, -indicator, -indicatorcmd, -itemtype, -padx, -pady, -relief, -selectbackground,

-selectborderwidth, -selectforeground, -selectmode, -separator, -sizecmd, -takefocus, -wideselection, -width, -xscrollcommand, -yscrollcommand

Label

Noninteractive widget displaying an image or text

-activetile, -anchor, -background, -bd, -bg, -bitmap, -borderwidth, -cursor,

-disabledtile, -fg, -font, -foreground, -height, -highlightbackground, -highlightcolor,

-highlightthickness, -image, -justify, -offset, -padx, -pady, -relief, -takefocus, -text,

-textvariable, -tile, -underline, -width, -wraplength

LabEntry

An Entry with a Label

Same as Entry plus -label, -labelPack, -labelVariable

LabFrame

A Frame with a Label

Same as Frame, plus -labelside

Listbox

List of text strings

-background, -bd, -bg, -borderwidth, -cursor, -exportselection, -fg, -font,

-foreground, -height, -highlightbackground, -highlightcolor, -highlightthickness,

-offset, -relief, -selectbackground, -selectborderwidth, -selectforeground,

-selectmode, -setgrid, -takefocus, -tile, -width, -xscrollcommand, -yscrollcommand

MainWindow

TopLevel widget displayed by MainLoop

-background, -bd, -bg, -borderwidth, -class, -colormap, -container, -cursor, -fg,

-foreground, -height, -highlightbackground, -highlightcolor, -highlightthickness,

-menu, -offset, -overanchor, -popanchor, -popover, -relief, -screen, -takefocus, -tile,

-title, -use, -visual, -width

Menu

Emergent window displaying menu items

-activebackground, -activeborderwidth, -activeforeground, -activetile, -background,

-bd, -bg, -borderwidth, -cursor, -disabledforeground, -disabledtile, -fg, -font,

-foreground, -offset, -overanchor, -popanchor, -popover, -postcommand, -relief,

-selectcolor, -takefocus, -tearoff, -tearoffcommand, -tile, -title, -type

Menubutton

Individual menu items

Same as Label plus -menu, -state, -disabledforeground, -direction, -activeforeground, -indicatoron, -activebackground

Message

Displays message for user

-anchor, -aspect, -background, -bd, -bg, -borderwidth, -cursor, -fg, -font, -foreground, -highlightbackground, -highlightcolor, -highlightthickness, -justify, -padx, -pady,

-relief, -takefocus, -text, -textvariable, -tile, -width

NoteBook

Groups tabbed windows together

-background, -backpagecolor, -bd, -bg, -borderwidth, -cursor, -disabledforeground,

-dynamicgeometry, -fg, -focuscolor, -font, -foreground, -inactivebackground, -ipadx,

-ipady, -relief, -slave, -tabpadx, -tabpady, -takefocus, -width

Optionmenu

Menu offering the user many mutually exclusive options

Same as Menubutton plus -variable, -command, -options

Pane

A scrollable Frame

Same as Frame plus -sticky, -xscrollcommand, -gridded, -yscrollcommand

Photo

Holds an image

-data, -format, -file, -gamma, -height, -palette, -width

ProgressBar

Shows user the “percentage complete”

Same as Canvas plus -colors, -troughcolor, -value, -to, -blocks, -variable, -foreground,

-from, -length, -resolution, -fg, -gap, -anchor, -padx, -pady

Radiobutton

A Button that, when selected, deselects the rest in the group

Same as Label plus -value, -selectcolor, -variable, -state, -command,

-disabledforeground, -activeforeground, -indicatoron, -selectimage,

-activebackground

ROText

Text widget, read-only

Same as Text

Scale

Slider for controlling a number

-activebackground, -activetile, -background, -bigincrement, -bd, -bg, -borderwidth,

-command, -cursor, -digits, -fg, -disabledtile, -font, -foreground, -from,

-highlightbackground, -highlightcolor, -highlightthickness, -label, -length, -offset,

-orient, -relief, -repeatdelay, -repeatinterval, -resolution, -showvalue, -sliderlength,

-sliderrelief, -state, -takefocus, -tickinterval, -tile, -to, -troughcolor, -troughtile,

-variable, -width

Scrollbar

Slider for moving a widget horizontally or vertically

-activebackground, -activerelief, -activetile, -background, -bd, -bg, -borderwidth,

-command, -cursor, -elementborderwidth, -highlightbackground, -highlightcolor,

-highlightthickness, -jump, -orient, -relief, -repeatdelay, -repeatinterval, -takefocus,

-tile, -offset, -troughcolor, -troughtile, -width

Table

Displays a table of items

Same as Frame plus -rows, -scrollbars, -fixedcolumns, -fixedrows, -columns

Text

Displays text, editable by user

-background, -bd, -bg, -borderwidth, -cursor, -disabledtile, -exportselection, -fg,

-font, -foreground, -height, -highlightbackground, -highlightcolor,

-highlightthickness, -insertbackground, -insertborderwidth, -insertofftime,

-insertontime, -insertwidth, -offset, -padx, -pady, -relief, -selectbackground,

-selectborderwidth, -selectforeground, -setgrid, -spacing1, -spacing2, -spacing3,

-state, -tabs, -takefocus, -tile, -width, -wrap, -xscrollcommand, -yscrollcommand

TextUndo

Text widget suited for writing documents

Same as Text

Tiler

A Frame of matrix cells

Same as Frame plus -rows, -columns, -yscrollcommand

TList

Text list

-background, -bd, -bg, -highlightbackground, -borderwidth, -browsecmd, -command, -cursor, -fg, -font, -foreground, -height, -highlightcolor, -highlightthickness,

-itemtype, -orient, -padx, -pady, -relief, -selectbackground, -selectborderwidth,

-selectforeground, -selectmode, -state, -sizecmd, -takefocus, -width,

-xscrollcommand, -yscrollcommand

Toplevel

Decorated container for other widgets

Same as MainWindow

Tree

HList that lets user open and close portions

Same as HList plus -opencmd, -ignoreinvoke, -closecmd

In the next article, Getting Started with Perl/Tk, Steve provides a gentle introduction to show you what some simple Perl/Tk programs look like.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.206.169