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.

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): ...

Get Mastering Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.