JavaScript EditorFree JavaScript Editor     Perl Manuals 

Main Page

4.20. Configuring a Button

Typically, you create a widget and then display it at some later time. What we haven't explicitly talked about yet is what you can do in between.

All options do not have to be specified when you create a widget. You can configure a widget further at a later time using the configure method, as long as you still have a reference to the widget. In addition, you can find out how a widget is already configured using the cget method. Here's an example:

$b = $mw->Button(-text => "Self referencing Button")->pack;
$b->configure(-command => [\&send_button, $b]);

In this example, we needed to use the actual widget reference in the callback for -command. We couldn't create the callback properly without the widget reference, so split that out using the configure method.[14] The configure and cget methods are generic to all widgets and are covered in Chapter 13, "Miscellaneous Perl/Tk Methods".

[14] Actually, we could create the callback as the widget is created if the widget reference was predeclared with a my $b statement.

To determine the current value of an option, call cget with only that option:

$state = $button->cget(-state);          # Get the current value for -state

To change the value associated with an option after the widget has been created, you call configure with the option and new value:

$button->configure(-text => "New Text"); # Change the text
$text = $button->cget(-text);            # Get the current text value

To get a list of lists describing every option and value for a widget, call configure without any arguments:

@all = $button->configure( );            # Get info on all options for Button
foreach $list (@all) {                    # Print options, not very pretty
	print "@$list\n";

JavaScript EditorJavaScript Formatter     Perl Manuals