PHP Dark Arts: GUI Programming with GTK

Note:  The examples mentioned in this article have source code available here.

PHP is not meant for desktop graphics programming.  It just isn’t.  PHP is web development language, or as you’ve seen in the PHP Dark Arts series, it can be used for some non-web related purposes.  But using PHP for GUI development just isn’t something people do, so naturally I had to give it a shot.  After much Google-fu I cam across the PHP-GTK project.

PHP GTK Logo
According to the PHP-GTK project site:

PHP-GTK is an extension for the PHP programming language that implements language bindings for GTK+. It provides an object-oriented interface to GTK+ classes and functions and greatly simplifies writing client-side cross-platform GUI applications

What Can You Do With It?

The first question that I had when I started looking at PHP-GTK was “What can I do with this?”.  Simple GUIs are very possible with PHP-GTK.  For example, you could easily build a questionnaire, a calculator, some sort of text editor, maybe a music library manager.  In short, you can build simple desktop applications.

Where To Get PHP-GTK

Getting PHP-GTK is easy.  Making it work for you is another matter.  I had originally hoped to get it running on my Ubuntu 9.10 virtual machine that I do all of my Re-CycledAir development work on, but that just wasn’t working for me.  I kept running into dependency issues and it just wouldn’t compile right.  After much pain and suffering I decided to just use the pre-compiled Windows binary which worked like a charm.  If you plan to follow any of these tutorials, I highly recommend that you use the Windows binary.

Once you have the Windows binary installed, just execute your GUI PHP programs using <path/to/phpgtk/>php.exe your_file.php.

Hello World

When I learn something new, be it a language or a library, I always like to start off with a simple “Hello World” program.  What follows is a simple “Hello World” program that creates a small window and displays some text.

set_title('Hello Re-CycledAir');
 
//Tell GTK to quit the main loop when we close the window.  This
//is what allows the program to exit fully.
$window-&gt;connect_simple('destroy', array('gtk', 'main_quit'));
 
//Create a simple label that displays "Hello Re-CycledAir!" and then
//add it to the window.
$labelHello = new GtkLabel("Hello Re-CycledAir!");
$window-&gt;add($labelHello);
 
//Make this window visible.
$window-&gt;show_all();
 
//Start the main loop.
Gtk::main();
?&gt;

If everything goes correctly, you should get something that looks like this.

Hello World PHP-GTK
Hello World 2

In this example, we place a button on this window instead of a label, which then triggers a modal pop-up window with a message.

//Check to see if PHP-GTK has been loaded correctly.
if (!class_exists('gtk')) {
     die("PHP-GTK has not been loaded in your php.ini file.");
}
 
//Create a new window and set it's title to "Hello Re-CycledAir".
$window = new GtkWindow();
$window-&gt;set_title('Hello Re-CycledAir');
 
//Tell GTK to quit the main loop when we close the window.  This
//is what allows the program to exit fully.
$window-&gt;connect_simple('destroy', array('gtk', 'main_quit'));
 
//Add an OK button, connect it to the ok function, and
//then add it to the window.
$buttonOK = new GtkButton("_OK");
$buttonOK-&gt;connect_simple('clicked', 'ok', $window);
$window-&gt;add($buttonOK);
 
//Make this window visible.
$window-&gt;show_all();
 
//Start the main loop.
Gtk::main();
 
function ok(GtkWindow $window) {
     $message = "Hello again!";
 
     //Create message dialog.
     $dialog = new GtkMessageDialog($window, Gtk::DIALOG_MODAL,
     Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, $message);
     $dialog-&gt;set_markup(
     "You have received this message: \r\n"
      . "<span>" . $message . "</span>"
     );
 
     //Run the dialog
     $dialog-&gt;run();
     $dialog-&gt;destroy();
 
     //Destroy the original window.
     $window-&gt;destroy();
}
?&gt;

If all goes well with this example, you should get output that looks like this:

PHP-GTK ModalConlcusions

I am by no means a good GUI programmer.  In fact, the only GUI programming I have done outside of these examples is building a few simple interfaces using Swing in Java.  Your mileage may vary, but I found PHP-GTK to be a bit clumsy in how it handles things.  Perhaps with some more development time it could become a better library, but generally there are much better languages and libraries out there for GUI development.

Note:  The examples mentioned in this article have source code available here.

Did you like this article?  You’ll probably like these too.