There are two ways of creating a button. You can create an empty
button and add the child later, or you can create a button with a
label as the child. The second form is provided as a convenience.
The following functions create a button:
$button = new Gtk::Button();
The first function above creates an empty Button. The last two
both create one with a label. The middle form is really just a
shortcut for the third.
$button = new Gtk::Button( $label );
$button = new_with_label Gtk::Button( $label );
If you create a button with a label, you can use
$button->child
to access the child widget. For example, to change the text in
the label, you could use:
$button->child->set( "new label" );
(see the section on
labels
for more information about labels).
If you do not create a button with a label, you would instead take
a widget and add it to the button. For example, the following
three sections of code are equivelent:
# create a button with a label
$button = new Gtk::Button( "text" );
# same as above, but with different new() function
$button = new_with_label Gtk::Button( "text" );
# create a label seperate from the button, and add it manually
$button = new Gtk::Button();
$label = new Gtk::Label( "text" );
$button->add( $label );
The Button widget has the following signals:
'pressed' - emitted when pointer button is pressed within Button widget or when the $button->pressed() function is called.
'released' - emitted when pointer button is released within Button widget or when the $button->released() function is called.
'clicked' - emitted when pointer button is pressed and then released within Button widget, or when the $button->clicked() function is called.
'enter' - emitted when pointer enters Button widget or when the $button->enter() function is called.
'leave' - emitted when pointer leaves Button widget or when the $button->leave() function is called.
Button relief styles can be one of the following:
'normal',
'half',
and
'none'.
As should be obvious,
'normal'
is the default. To get and set the relief style, you would use
the following functions:
$button->set_relief( $relief_style );
$button->get_relief();
In the following program, I first create a Button with a label and attach a signal handler, and later I remove that Button and replace it with one created without a Label, and add a Label manually. You should be able to understand both what is going on with the Buttons and with the signal handling before going further.
#!/usr/bin/perl -w use Gtk; use strict; set_locale Gtk; init Gtk; my $false = 0; my $true = 1; my $window = new Gtk::Window( "toplevel" ); my $button = new Gtk::Button( "Button created with label" ); my $label; my $id; my $numclicked = 0; # callback registration $window->signal_connect( "delete_event", \&CloseWindowEvent ); $button->signal_connect( "clicked", \&ClickedButtonEvent ); # button attributes $button->show(); # window attributes $window->border_width( 15 ); $window->add( $button ); $window->show(); # Gtk event loop main Gtk; exit( 0 ); ### Subroutines # Callback called when the button is clicked. The first time it is # clicked, the button label changes. The second time a new label is # added to the button. The third time, the label is changed. And the # fourth time the button is clicked, the program exits. sub ClickedButtonEvent { if ( $numclicked == 0 ) { $button->child->set( "Changed Button Label" ); $numclicked++; } elsif ( $numclicked == 1 ) { $window->remove( $button ); $button = new Gtk::Button(); $label = new Gtk::Label( "Label added to a button" ); $button->add( $label ); $label->show(); $button->show(); $window->add( $button ); $id = $button->signal_connect( "clicked", \&ClickedButtonEvent ); $numclicked++; } elsif ( $numclicked == 2 ) { $label->set( "Now Click to Close Window" ); $numclicked++; } else { Gtk->exit( 0 ); } } # Callback called when the window is asked to close. What really # happens is that the window moves down and to the right. This is an # excellent example of a program that doesn't act like the user # expects. sub CloseWindowEvent { # If you return a false value in the "delete_event" signal # handler, GTK will emit the "delete_event" signal. # Returning a true value means you don't want the window to # be destroyed. $window->window->move( 100, 100 ); return $true; } # END EXAMPLE PROGRAM
Below are four screenshots showing the different states of the example program:

Button Example State 1 Screenshot

Button Example State 2 Screenshot

Button Example State 3 Screenshot

Button Example State 4 Screenshot