O'Reilly logo

Mastering Perl/Tk by Nancy Walsh, Stephen Lidie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating Button Widgets

As with any widgets you create, you call a method from the parent widget that matches the name of the widget:

$button = $mw->Button->pack;
$rb = $mw->Radiobutton->pack;
$cb = $mw->Checkbutton->pack;

These are unrealistic examples as you will most likely use some options when creating each different Button type:

# Create a Button widget
$mw->Button(-text => 'Go', -command => \&go_go_go)->pack;

# Create a Checkbutton
$cb = $mw->Checkbutton(-text => 'Red', -onvalue => 'Red', 
	-offvalue => '')->pack;

# Create three Radiobuttons in Frame widget $f1
# Link them using $favcolor
foreach (qw/red blue green/) {
    $f1->Radiobutton(-text => $_, -variable => \$favcolor, 
        -value => $_)->pack(-anchor => 'w');
}

We’ll explain the options used in the previous examples in upcoming sections. In particular, -command expects a callback, which we’ll mention briefly in Section 4.9, but we won’t fully describe until Chapter 15.

The only time you might not want to save a reference is when you create a Button, set the text, and set a simple callback for it all at once:

$mw->Button(-text => 'Quit', -command => sub { print 'Bye!'; exit; })->pack;

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required