Categories
PHP

php-gtk sample app

In my previous posts I wrote about php-gtk. Just a quick reminder: php-gtk lets you build desktop GUI applications for the GTK platform. These applications can be deployed on multiple platform without any hassle.

Here’s the source codes of a very simple “Hello, World!” application in php-gtk:

 

<?php

if (!class_exists(‘gtk’)) {

die(“Please load the php-gtk2 module in your php.inirn”);

}

 

$wnd = new GtkWindow();

$wnd->set_title(‘Hello world’);

$wnd->connect_simple(‘destroy’, array(‘gtk’, ‘main_quit’));

 

$lblHello = new GtkLabel(“Just wanted to sayrn’Hello world!'”);

$wnd->add($lblHello);

 

$wnd->show_all();

Gtk::main();

?>

 

If you go through the codes, you will get the structure very easily.

  • if (!class_exists(‘gtk’)) { die(“Please load the php-gtk2 module in your php.inirn”);}
    This line just checks if php-gtk is installed.
  • $wnd = new GtkWindow();
    Creates a new window.
  • $wnd->set_title(‘Hello world’);
    Sets the window title.
  • $wnd->connect_simple(‘destroy’, array(‘gtk’, ‘main_quit’));
    A bit complex. Connects the main_quit method of gtk with the “destroy” event of the window.
  • $lblHello = new GtkLabel(“Just wanted to sayrn’Hello world!'”);
    Creates a new label with the predefined text.
  • $wnd->add($lblHello);
    Adds the label to the main window.
  • $wnd->show_all();
    Shows all components.
  • Gtk::main();
    Starts the main loop. The application runs.

 

That was easy, wasn’t it?