Book HomeMastering Perl/TkSearch this book

Chapter 13. Miscellaneous Perl/Tk Methods

Contents:

Managing Widgets with configure and cget
Building a Family Tree
Widget's ID
Color-Related Methods
The Application's Name
Widget Existence
Is the Widget Mapped?
Converting Screen Distances
Size of Widget
Widget Position
Screen Information
Atom Methods
Ringing a Bell
Clipboard and Selection Methods
Destroying a Widget
Focus Methods
Grab Methods
Marking a Widget Busy and Unbusy
Widget Mapping and Layering
Interapplication Communication
Waiting for Events to Happen
Time Delays
Parsing Command-Line Options
Really Miscellaneous 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:

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:

0
Option name

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

2
Class in the option database

3
Default value of the option

4
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:

['-activebackground',activeBackground,Foreground,'#ececec','#ececec'],
['-activeforeground',activeForeground,Background,Black,Black],['-activeimage',
activeImage,ActiveImage,undef,undef],['-anchor','anchor',Anchor,'center',
'center'],['-background','background',Background,'#d9d9d9','#d9d9d9'],['-bd',
borderWidth],['-bg','background'],['-bitmap','bitmap',Bitmap,undef,undef],
['-borderwidth',borderWidth,BorderWidth,2,2],['-command','command',Command,
undef,bless([CODE(0x8189888)],Tk::Callback)],['-cursor','cursor',Cursor,
undef,undef],['-disabledforeground',disabledForeground,DisabledForeground,
'#a3a3a3','#a3a3a3'],['-fg','foreground'],['-font','font',Font,'-Adobe
-Helvetica-Bold-R-Normal--*-120-*-*-*-*-*-*','-Adobe-Helvetica-Bold-R-Normal
--*-120-*-*-*-*-*-*'],['-foreground','foreground',Foreground,Black,Black],
['-height','height',Height,0,0],['-highlightbackground',highlightBackground,
HighlightBackground,'#d9d9d9','#d9d9d9'],['-highlightcolor',highlightColor,
HighlightColor,Black,Black],['-highlightthickness',highlightThickness,
HighlightThickness,2,2],['-image','image',Image,undef,undef],['-justify',
'justify',Justify,'center','center'],['-padx',padX,Pad,3m,9],['-pady',padY,
Pad,1m,3],['-relief','relief',Relief,'raised','raised'],['-state','state',
State,'normal','normal'],['-takefocus',takeFocus,TakeFocus,undef,undef],
['-text','text',Text,undef,Do_Something],['-textvariable',textVariable,
Variable,undef,undef],['-underline','underline',Underline,-1,-1],['-width',
'width',Width,0,0],['-wraplength',wrapLength,WrapLength,0,0]

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 = [
           '-highlightthickness',
           'highlightThickness',
           'HighlightThickness',
           '1',
           1
         ];

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:

$widget->cget(-option)

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:
2
# 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.