Saturday, January 9, 2016

How to use Glade3, Gtk+3 and Perl 5 to create GUI Applications

First, we will need to install the required dependencies. So make sure you install the following dependencies:
  • libgtk3-perl
  • glade
You can install them via a Package manager or the console. If you are using the console, copy and paste the following into the command line:
sudo apt-get install glade libgtk3-perl -y
They say an image is worth a thousand words. So here is an image that illustrates exactly what you should do next. I'll go over the main points.

Glade and Perl


On the right is GVim and on the left is the Glade editing interface.

First, make sure you
use Gtk3 qw(-init);
To initialize Gtk. You do not need to create a window if your layout is contained within one, like mine. All you have to do is:

my $builder = Gtk3::Builder->new();
$builder->add_from_file("./path_to_glade_savefile.glade");
$builder->connect_signals(undef);
my $window_1 = $builder->get_object("window1");
$window_1->show_all;
Gtk3::main;

Basically, you create a builder, feed it the glade project save file, then you take out the windows you want (in my case that is "window1" and "window2") and finally, you "show" the ones you want to be visible.

Also, as demonstrated in the image, you can use package syntax (e.g. Handlers::my_callback) to link the signals. This extremely useful, since you can segregate functionality into multiple packages and keep things relatively clean.


If you feel I missed a point, the example is not clear enough or there are factual errors in the article, please let me know in the comment section so that I can fix them. Thanks for reading!

No comments:

Post a Comment