This site has been retired. For up to date information, see handbook.gnome.org or gitlab.gnome.org.


[Home] [TitleIndex] [WordIndex

GTK-Perl is the collective name for a set of perl bindings for GLib, GTK+, and various related libraries. These modules make it easy to write Gtk and Gnome applications using a natural, perlish, object-oriented syntax.

GTK+ is a GUI toolkit for developing graphical applications that run on POSIX systems such as Linux, Windows and MacOS X (provided that an X server for MacOS X has been installed). It provides a comprehensive set of widgets, and supports Unicode and bidirectional text. It links into the Gnome Accessibility Framework through the ATK library.

Perl is a stable, multi-platform programming language, used throughout the entire Internet and in many mission-critical environments.

GTK-Perl Resources

GTK-Perl Documentation

GTK-Perl Modules

GNOME Platform Bindings Modules

The GTK+ 2.x bindings are static and use Perl's XS language.

The GTK+ 3.x bindings are dynamically generated through Glib::Object::Introspection.

The following bindings belong to the GNOME 2.x platform, and are considered deprecated. They should not be used in newly written code.

Other Modules

GTK-Perl Development

The modules that make up GTK-Perl are hosted on GNOME git. To get the repository name from the module's main package, prepend 'perl-' and replace '::' with '-'. For example, Glib lives in the perl-Glib repository and Gnome2::Canvas in perl-Gnome2-Canvas.

To check out a module, use:

git clone git://git.gnome.org/<module>

GTK-Perl "Hello, World!" Program

use strict;
use warnings;
use v5.18;

use Glib ':constants';   # load Glib and import useful constants
use Gtk3 '-init';        # load Gtk3 module and initialize it

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

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

# Sets the border width of the window.
$window->set_border_width(10);

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

$button->signal_connect(clicked => sub {
        my ($button) = @_;

        say "Hello, World!";
        
        # We can use variables defined outside the sub's scope, thanks
        # to Perl closures.
        $window->destroy();
    });

# This packs the button into the window (a gtk container).
$window->add($button);

# The final step is to display this newly created widget.
$button->show();

# and the window
$window->show();

# 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).
Gtk3::main();

# Once the main_quit() method stops the main loop, control is returned to this
# point. In this case, we simply exit successfully.
0;

2024-10-23 11:36