Learning PERL/Tk: Graphical User Interfaces with Perl by Nancy Walsh, Paperback | Barnes & Noble
Learning PERL/Tk: Graphical User Interfaces with Perl

Learning PERL/Tk: Graphical User Interfaces with Perl

by Nancy Walsh
     
 

Learning Perl/Tk is a tutorial for Perl/Tk, the extension to Perl for creating graphical user interfaces. With Tk, Perl programs can be window-based rather than command-line based, with buttons, entry fields, listboxes, menus, and scrollbars. Originally developed for the Tcl language, the Perl port of the Tk toolkit liberates Perl programmers from the world

Overview

Learning Perl/Tk is a tutorial for Perl/Tk, the extension to Perl for creating graphical user interfaces. With Tk, Perl programs can be window-based rather than command-line based, with buttons, entry fields, listboxes, menus, and scrollbars. Originally developed for the Tcl language, the Perl port of the Tk toolkit liberates Perl programmers from the world of command-line options, STDIN, and STDOUT, allowing them to build graphical, event-driven applications for both Windows and UNIX.This book is aimed at Perl novices and experts alike. It explains the reasoning behind event-driven applications and drills in guidelines on how to best design graphical applications. It teaches how to implement and configure each of the Perl/Tk graphical elements step-by-step. Special attention is given to the geometry managers, which are needed to position each button, menu, label and listbox in the window frame.Although this book does not teach basic Perl, anyone who has written even the simplest Perl program should be able to learn Tk from this book. The writing is breezy and informal, and gets right to the point of what you need to know and why. The book is rife with illustrations that demonstrate how each element is drawn and how its configuration options affect its presentation.Learning Perl/Tk is for every Perl programmer who would like to implement simple, easy-to-use graphical interfaces.

Product Details

ISBN-13:
9781565923140
Publisher:
O'Reilly Media, Incorporated
Publication date:
01/01/1998
Pages:
373
Product dimensions:
6.97(w) x 9.13(h) x 0.76(d)

Read an Excerpt


Chapter 5: Label and Entry Widgets Label Configuration

Label is a pretty boring widget, so there are only two methods available to change or get information on it: cget and configure. Both methods work for Label the same way they work for the Button widget. Please refer to Appendix A for the details on arguments and return values.

The Entry Widget

ENTRY WIDGET: Until now, the only input we know how to get from the input from a mouseclick is useful, but it's also limiting. The entry widget will let the user type in text that can then be used, in, any way by the application. Here are a few examples of where you might use an entry widget:

  • In a database form that requires one entry per field (e.g
  • In a software window that requires a serial number.
  • In a login window that requires a username and password
  • In a configuration window to get the name of a printer
  • In an Open File window that requires the path and name of a file

Normally, we don't care what users type in an entry widget until they are done typing, and any processing will happen "after the fact" when a user clicks some sort of Go button. You could get fancy and process each character as it's typed by setting up a complicated bind-but it is probably more trouble than it is worth.

The user can type anything into an entry widget. It is up to the programmer to decide if the text entered is valid or not. When preparing to use the information from an entry, we should do some error checking. If we want an integer and get some alphabetic characters, we should issue a warning or error message to the user.

An entry widget is a much more complex widget than it first appears to be. The entry widget is really a simplified one-line text editor. Text can be typed in, selected with the mouse, deleted, and added. I would classify an entry widget as a middle-of-the-line widget. It's more complicated than a button, but much less complicated than the text or canvas widgets.

Creating the Entry Widget No surprises here:

$entry = $parent->Entry( [ option => value . . . ] )->pack;

When the entry widget is created, it is initially empty of any text, and the insert cursor (if the entry had the keyboard focus) is at the far-left side.

Entry Options

The following list contains a short description of each option available for configuring an entry widget. Several of them are discussed in more detail later in this chapter.

-background => color

Sets the background color of the entry widget. This is the area behind the text.

-borderwidth => amount

Changes the width of the outside edge of the widget. Default value is 2.

-cursor => cursorname

Changes the cursor to cursorname when it is over the widget.

-exportselection => 0 1 1

If the Boolean value specified is true, any text selected will be exported to the windowing system's clipboard.

-font => fontname

Changes the font displayed in the entry to fontname.

-foreground => color

Changes the color of the text.

-highlightbackground => color

Sets the color the highlight rectangle should be when the widget does not have the keyboard focus.

-highlightcolor => color

Sets the color the highlight rectangle should be when the widget does have the keyboard focus.

-highlightthickness => amount

Sets the thickness of the highlight rectangle around the widget. Default is 2.

-insertbackground => color Sets the color of the insert cursor.

-insertborderwidth => amount

Sets the width of the insert cursor's border. Normally used in conjunction with

-ipadx and -ipady options for the geometry manager.

-insertoff time => milliseconds

Sets the amount of time the insert cursor is off in the entry widget.

-insertontime => milliseconds

Sets the amount of time the insert cursor is on in the entry widget.

-insertwidth => amount

Sets the width of the insert cursor. Default is 2.

-justify => left, 1 'right' 1 'center'

Sets the justification of the text in the entry widget. The default is left.

-relief => flat, 1 'groove, 1 raised, 1 ridge, sunken,

Sets the relief of the outside edges of the entry widget.

-selectbackground => color

Sets the background color of any selected text in the entry widget.

-selectborderwidth => amount

Sets the width of the selection highlight's border.

-selectforeground => color

Sets the text color of any selected text in the entry widget.

-show => char

Sets the character that should be displayed instead of the actual text typed.

-state => 'normal' 1 disabled, 1 active,

Indicates the state of the entry. Default is 'normal'

-takefocus => 0 1 1 1 undef

Allows or disallows this widget to have the keyboard focus.

-textvariable => \$variable

Sets the variable associated with the information typed in the entry widget.

-width => amount

Sets the width of the entry in characters.

-xscrollcommand => callback Assigns a callback to use when scrolling back and forth.

The following options behave as we expect them to, and aren't worth further discussion: - background, -cursor, -font, -highlightbackground, -highlightcolor, -highlightthickness, -foreground, - justify, -takefocus, and -state. For more detailed information on these how these options affect a widget, see Chapter 3.

Assigning the Entry's Contents to a Variable

The -textvariable option lets you know what the user typed in the entry widget:

-textvariable => \$variable

By now, you should be familiar with this option from several of our button examples. Any text input into the entry widget will get assigned into $variable. The reverse also applies. Any string that gets assigned to $variable will show up in the entry widget.

It is important to remember that no matter what the user enters, it will be assigned to this variable. This means that even though you are expecting numeric input (e.g., "314"), you might get something like "3s14" if the user accidentally (or on purpose!) presses the wrong key(s). Before using any information from an entry widget, it's a good idea to do some error checking to make sure it's the information you expect or, at the very least, in the correct format. Trying to use "3s14i" in an equation would most likely produce undesired results.

The other way you can find out what is in the entry widget is by using the get method:

$stuff = $entry->geto;

You can use get whether or not you have used the -textvariable option....

Meet the Author

Nancy Walsh has been involved with Perl (and Perl/Tk) since 1996. She received a Computer Science degree from the University of Arizona in 1993, and currently works as a Principal Consultant for XOR, Inc. doing J2EE and Java work on various projects. Nancy has taught several Perl/Tk Tutorials at the O'Reilly Open Source Conference and is also the author of Learning Perl/Tk.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >