home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeMastering Perl/TkSearch this book

Chapter 13. Miscellaneous Perl/Tk Methods

So far, most of the chapters in this book have concentrated on specific widgets. This chapter covers the methods and subroutines that can be invoked from any widget. You'll probably never need most of these methods, but there are a few that you'll use frequently, particularly configure and cget.

Most of the methods are based on the Tcl winfo command (window information). Generally, the commands are informational only, meaning you pass no arguments to them; you only get a value back.

This chapter also documents clipboard and selection methods for cut-and-paste operations between applications, focus and grab methods, and a few esoteric goodies.

13.1. Managing Widgets with configure and cget

Every widget included in the Perl/Tk distribution, as well as all user-contributed widgets available separately, can use the configure and cget methods, which set, change, and query widget attributes. No matter the widget, the format of the arguments to these functions is the same, and the results passed back have the same format.

The configure method allows you to assign or change the value of a widget option. It can also be used to retrieve the current value of the option. The cget method cannot assign values but simply retrieves them with simpler syntax than that of configure.

13.1.1. The configure Method

The basic format of the configure method is as follows:

$widget->configure( [ option => newvalue, ... ] );

Depending on the arguments passed to it, the configure method can do three things:

  • Set or change the values of the options for $widget.

  • Get the current value of any option for $widget.

  • Get the current values of all of the options for $widget.

To set or change the value of an option, send the option pair exactly as it would have appeared in the widget creation command:

$widget->configure(-option => newvalue);

Whatever effect the option has will take place immediately. To see the current values for a single option, send it as the argument. The return value depends on whether configure is called in list context or scalar context. In the following line, configure is called in list context (since its return value is being assigned to an array):

@info = $widget->configure(-highlightthickness);

In list context, an array of scalars is returned:

-highlightthickness highlightThickness HighlightThickness 2 2

The following five values are in the returned array:

Option name

Option name from the option database (also as it would appear in the .Xdefaults file)

Class in the option database

Default value of the option

Current value of the option

Calling configure in scalar context returns a reference to the five configure items.

If you want to see the list of values for all the options the widget supports, use this format:

@config = $widget->configure;

@config is now an array of anonymous lists. The easiest way to print this information is to utilize Tk::Pretty, which will do all the hard work of traversing the arrays and then put the information into a readable form:

use Tk::Pretty;
@config = $widget->configure;
print Pretty @config;

The result is as follows:


Although this list may look nasty and ugly, it distinguishes between the different lists of lists for you by adding the [ and ] characters and the commas that separate them. Usually, you would look at this list only for debugging purposes.

You may find the output of Data::Dumper more pleasing:

use Data::Dumper;
@config = $widget->configure;
print Dumper @config;

Producing output similar to this:

$VAR22 = [

The configuration options for each widget described in this book are listed in Appendix B, " Options and Default Valuesfor Each Widget".

13.1.2. The cget Method

Instead of using configure to retrieve values, you can use the cget method:


It returns only the current value (or address if the option stores a reference) of the option rather than the entire list that configure returns. Think of cget as standing for "configuration get." Here is an example of how to use cget:

print $b->cget(-highlightthickness), "\n";
## Prints this:
# return reference :
print $option_menu->cget(-textvariable), "\n";
# return actual value :
print ${$option_menu->cget(-textvariable)}, "\n";
# or...
$ref = $option_menu->cget(-textvariable);
print $$ref, "\n";

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.