Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

4.1. 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;

					  

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint