4. Stepping Through Hello World

Now that we know the theory behind this, let's clarify by walking through the example helloworld program.

First of all, if we want to use the symbolic names of TRUE and/or FALSE inside our Perl programs, instead of the generic integers 1 or 0, we should include the Glib module:

use Glib qw/TRUE FALSE/;

The next use pragma should be used in every Perl program which accesses the GTK2-Perl bindings. We require the Gtk2 module, and we tell this module to automatically call the init() method upon inclusion, using the -init parameter.

use Gtk2 '-init';

Here is the callback function that will be called when the button is "clicked". We use the second argument of the callback to pass the window object inside the hello() function, where we call the destroy() method upon it, in order to close our program.

sub hello {
	my ($widget, $window) = @_;
	print "Hello, World\n";


The next callback is a bit special. The "delete_event" occurs when the window manager sends this event to the application. We have a choice here as to what to do about these events. We can ignore them, make some sort of response, or simply quit the application.

The value you return in this callback lets GTK know what action to take. By returning TRUE, we let it know that we don't want to have the "destroy" signal emitted, keeping our application running. By returning FALSE, we ask that "destroy" be emitted, which in turn will call our "destroy" signal handler.

sub delete_event
    print "delete event occurred\n";

    return TRUE; 

Create a new window. This is fairly straightforward. It sets up a new window, but it is not displayed until we call the show() method near the end of our program.

Notice the toplevel argument. In GTK2-Perl, the standard C enumerators are automatically translated into string literals and vice-versa. We could have used the constant GTK_WINDOW_TOPLEVEL, instead.

$window = Gtk2::Window->new('toplevel');

Here are two examples of connecting a signal handler to an object, in this case, the window. Here, the "delete_event" and "destroy" signals are caught. The first is emitted when we use the window manager to kill the window, or when we use the Gtk2::Widget::destroy() method passing in the window widget as the object to destroy. The second is emitted when, in the "delete_event" handler, we return FALSE.

In the second call of the signal_connect() method, we use one of the facilities offered by Perl: anonymous subs. You may have as many callback functions as you need, and all will be executed in the order you connected them.

$window->signal_connect(delete_event => \&delete_event);
$window->signal_connect(destroy => sub { Gtk2->main_quit; });

This next function is used to set an attribute of a container object. This just sets the window so it has a blank area along the inside of it 10 pixels wide where no widgets will go. There are other similar functions which we will look at in the section on Setting Widget Attributes

$window is an object which inherits from the Gtk2::Container class, so we can call any method of this class upon instances of the Gtk2::Window class.


This call creates a new button. It will have the label "Hello World" on it when displayed.

$button = Gtk2::Button->new("Hello World");

Here, we take this button, and make it do something useful. We attach a signal handler to it so when it emits the "clicked" signal, our hello() function is called. We pass the the callback the window reference. Obviously, the "clicked" signal is emitted when we click the button with our mouse pointer.

$button->signal_connect(clicked => \&hello, $window);

We want the button to close our program. This will illustrate how the "destroy" signal may come from either the window manager, or our program. When the button is "clicked", same as above, it calls the first hello() callback function, and that will call the destroy() method upon the window object.

This is a packing call, which will be explained in depth later on in Packing Widgets. But it is fairly easy to understand. It simply tells GTK that the button is to be placed in the window where it will be displayed. Note that a Gtk2::Container can only contain one widget. There are other widgets, that are described later, which are designed to layout multiple widgets in various ways.


Now we have everything set up the way we want it to be. With all the signal handlers in place, and the button placed in the window where it should be, we ask GTK to "show" the widgets on the screen. The window widget is shown last so the whole window will pop up at once rather than seeing the window pop up, and then the button form inside of it. Although with such a simple example, you'd never notice.



And of course, we call main() which waits for events to come from the X server and will call on the widgets to emit signals when these events come.


And the final return. Control returns here after gtk_quit() is called.


Now, when we click the mouse button on a GTK button, the widget emits a "clicked" signal. In order for us to use this information, our program sets up a signal handler to catch that signal, which dispatches the function of our choice. In our example, when the button we created is "clicked", the hello() function is called with a NULL argument, and then the next handler for this signal is called. This calls the gtk_widget_destroy() function, passing it the window widget as its argument, destroying the window widget. This causes the window to emit the "destroy" signal, which is caught, and calls our destroy() callback function, which simply exits GTK.

Another course of events is to use the window manager to kill the window, which will cause the "delete_event" to be emitted. This will call our "delete_event" handler. If we return TRUE here, the window will be left as is and nothing will happen. Returning FALSE will cause GTK to emit the "destroy" signal which of course calls the "destroy" callback, exiting GTK.