Programming with Gtk2-Perl/Getting Started/Hello World
Hello World!
[edit | edit source]use strict;
use warnings;
use Gtk2;
Gtk2->init;
my $window = Gtk2::Window->new;
my $label = Gtk2::Label->new('Hello World!');
$window->signal_connect('delete-event' => sub { Gtk2->main_quit });
$window->add($label);
$label ->show;
$window->show;
Gtk2->main;
Breaking it down
[edit | edit source]use strict;
use warnings;
Because if someone hasn't told you to include these at the top of every Perl program you write, I'm telling you now.
use Gtk2;
The Gtk+ libraries are included under the Gtk2 namespace.
Gtk2->init;
This method needs to be called from every Gtk2 application. It initializes the library for use, setting up things things like the color map and connecting the default signal handlers. This method also checks the arguments that have been passed to your application on the command line. The following arguments are accepted by all Gtk applications. They will be removed from the argument list, leaving the rest for your application to handle.
* --gtk-module * --g-fatal-warnings * --gtk-debug * --gtk-no-debug * --gdk-debug * --gdk-no-debug * --display * --sync * --name * --class
Instead of explicitly calling the init method yourself, you could call it implicitly like this.
use Gtk2 '-init';
my $window = Gtk2::Window->new('toplevel');
my $label = Gtk2::Label->new('Hello World!');
These two lines create new widgets. A widget is an element of the GUI. Windows, labels, buttons, menus, images are all examples of widgets. In this example we can see that the constructor to the window widget takes an argument telling it to make a top-level window, and the label widget can take some text to display. Different widgets accept different parameters to the constructor. Check the Gtk documentation for more information on specific widgets.
$window->add($label);
This line packs the label into the window. A window widget is an example of a container. A container widget is used to control how its children will be displayed on the screen. Container widgets have a variety of methods that can be used to add children. Here we are using a very basic example to display a label inside a window.
$label ->show;
$window->show;
Can you guess what these lines do?
$window->show_all;
We could have said this instead.
Gtk2->main;
Every Gtk application has must have a call to the main method. This turns control over the Gtk main loop which waits for events to occur and then takes the proper action.
$window->signal_connect('delete-event' => sub { Gtk2->main_quit });
We didn't forget to cover this line. Here we attach a callback to the 'delete-event' of the window. A callback is a reference to a function. In this example we use an anonymous subroutine, which interrupts Gtk's main loop and returns control back over to the program. Any code after the call to Gtk2->main will be executed, in our example the program just closes. Had we not connected this callback to the 'delete-event', the window would have closed but our program would still have been running in the main loop, and the user would have no easy way to exit the application. (You could kill it with Ctrl+c.)