Chapter 4. Getting Started

Table of Contents

1. Hello World in GTK2-Perl
2. Theory of Signals and Callbacks
3. Events
4. Stepping Through Hello World

To begin our introduction to GTK2-Perl, we'll start with the simplest program possible. This program will create a 200x200 pixel window and has no way of exiting except to be killed by using the shell.

use Gtk2;


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



You can run the above program using:


All programs will of course use the Gtk2 module.

The next line:


calls the init method which will be called in all GTK applications. This sets up a few things for us such as the default visual and color map. This method initializes the library for use, sets up default signal handlers, and checks the arguments passed to your application on the command line, looking for one of the following:

It removes these from the argument list, leaving anything it does not recognize for your application to parse or ignore. This creates a set of standard arguments accepted by all GTK applications.

The init method could be implicitly being run by using the '-init' paramater when loading the Gtk2 module, like this:

use Gtk2 '-init';

The next two lines of code create and display a window.

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

The toplevel argument specifies that we want the window to undergo window manager decoration and placement. Rather than create a window of 0x0 size, a window without childer is set to 200x200 by default so you can still manipulate it.

The show method lets GTK know that we are done setting the attributes of this widget, and that it can display it.

The last line enters the GTK main processing loop.


main is another call you will see in every GTK application. When control reaches this point, GTK will sleep waiting for X events (such as a button or key presses), timeouts, or gile IO notifications to occur. In our simple example, however, events are ignored.

1. Hello World in GTK2-Perl

Now for a program with a widget (a button). It's the classic hello world a la GTK.

# Use the TRUE and FALSE constants exported by the Glib module.
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

# This is a callback function. We simply say hello to the world, and destroy
# the window object in order to close the program.
sub hello
	my ($widget, $window) = @_;
	print "Hello, World\n";


sub delete_event
	# If you return FALSE in the "delete_event" signal handler,
	# GTK will emit the "destroy" signal. Returning TRUE means
	# you don't want the window to be destroyed.
	# This is useful for popping up 'are you sure you want to quit?'
	# type dialogs.
	print "delete event occurred\n";

	# Change TRUE to FALSE and the main window will be destroyed with
	# a "delete_event".
	return TRUE;

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

# When the window is given the "delete_event" signal (this is given
# by the window manager, usually by the "close" option, or on the
# titlebar), we ask it to call the delete_event () functio
# as defined above. No data is passed to the callback function.
$window->signal_connect(delete_event => \&delete_event);

# Here we connect the "destroy" event to a signal handler.
# This event occurs when we call Gtk2::Widget::destroy on the window,
# or if we return FALSE in the "delete_event" callback. Perl supports
# anonymous subs, so we can use one of them for one line callbacks.
$window->signal_connect(destroy => sub { Gtk2->main_quit; });

# Sets the border width of the window.

# Creates a new button with a label "Hello World".
$button = Gtk2::Button->new("Hello World");

# When the button receives the "clicked" signal, it will call the function
# hello() with the window reference passed to it.The hello() function is
# defined above.
$button->signal_connect(clicked => \&hello, $window);

# This packs the button into the window (a gtk container).

# The final step is to display this newly created widget.

# and the window

# All GTK applications must have a call to the main() method. Control ends here
# and waits for an event to occur (like a key press or a mouse event).