Gtk2-Perl
" Curiosity has overwhelmed you, and you want to see Gtk2-Perl
in action.
Start off by the typical 'Hello' program. Open your favorite editor and enter the following code in a file.
1 #! /usr/bin/perl -w 2 use strict; 3 use Gtk2 -init; 4 my $window = Gtk2::Window->new ('toplevel'); 5 $window->signal_connect (delete_event => sub { Gtk2->main_quit }); 6 7 my $button = Gtk2::Button->new ('Action'); 8 $button->signal_connect (clicked => sub { 9 10 print("Hello Gtk2-Perl\n"); 11 12 }); 13 14 $window->add ($button); 15 $window->show_all; 16 Gtk2->main;
Save it
Make it executable:
# chmod 755 (script name)
Execute it:
# ./(script name)
This is a very basic program used for introduction purposes.
#! /usr/bin/perl -wThis is a line that any executable perl program will start with. We call it the "she-bang" line.(#- she, !- bang). This is followed by the path where the perl executable can be found on your system. Please check it, by executing the following command:
# which perlThe reply should then be appended to the '#!' part. The '-w' is a directive to the Perl interpreter, telling it to enable useful warnings.
use strict;This is another directive to the Perl interpreter that forces good programming practice like variable declaration.
use Gtk2
-init;
Just about every Gtk2-Perl
script, which is not a module, should contain "use Gtk2 -init"
. This initialization should take place before using any other Gtk2
functions in your GUI applications. It will initialize everything needed to operate the toolkit and parse some standard command line options.
[1]
To show things on the screen, it is necessary to create a window. We create a window by:
my $window = Gtk2::Window
->new ('toplevel');
The new
method accepts as an argument the type of window to be created. Creating a window (or any other widget) does not show it on screen. To show it on the screen, you have to call the Gtk2::Widget
's show
method.
You can call the |
$window->signal_connect (delete_event => sub { Gtk2
->main_quit });
This connects the delete_event
signal to an anonymous sub-procedure, which calls the Gtk2
->main_quit
method. The delete_event
is emitted when the user closes the window.
my $button = Gtk2::Button
->new ('Action);
This creates a new object called $button from the Gtk2::Button
class. It also will create a label called 'Action' on the button.
$button->signal_connect (clicked => sub { print("Hello Gtk2-Perl\n"); });This connects the
clicked
signal to an anonymous sub-procedure, which prints "Hello Gtk2-Perl
".
$window->add ($button);$window is a subclass of
Gtk2::Container
. You add a widget to a container using the add
method. Adding more than one widget to a container is not trivial. The reason for this is that Gtk+ has not been designed to set widgets at a specific location in their parent widget.
(This is not completely true [2])
Instead, Gtk+ asks that you pack
your widgets in their parent widget. This means that if the parent widget gets resized, it's child widgets are resized as well, depending on the packing options that were set.
One of the reasons that the Gtk+ library was set up this way, is that the size of a widget is not well-defined. This means that a button's size depends on whether it is the default widget of the window or not. Given that this is so, the placement of such a button is not well-defined either.
The most common ways of packing widgets in a parent widget are the following:
using a vertical box
using a horizontal box
using a table
$window->show_all;Make all widgets inside $window visible by calling this method. In this case it is only $button.
Gtk2
->main;
This enters the main loop, which will cause the program to wait for user input via the mouse or keyboard. Later in the book we'll also use other means of input, IE network sockets or feedback from a database.
[1] |
| ||
[2] | |