001. How to get started?

Problem

How to get started in PHP-GTK2?

As with many other programming languages, let start with this simple yet complete "hello world!" PHP-GTK2 example as shown below:

How to get started?


Solution

Do take some time to understand this simple yet complete "hello world!" PHP-GTK2 script.

Once you understand this script, you are on your way to picking up other widgets one by one and add onto this script.


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
<?php
$window = new GtkWindow();
$window->connect_simple('destroy', array('Gtk','main_quit'));
$label = new GtkLabel("hello world!");
$window->add($label);
$window->show_all();
Gtk::main();
?>

Output

As shown above.
 

Explanation

  1. new GtkWindow() creates a new window.
  2. connect('destroy', array('Gtk','main_quit')) ensures a clean exit when you close the window. This basically says call Gtk::main_quit() when the user close the window.
  3. new GtkLabel("hello world!") creates the text string as label.
  4. And we use $window->add to place the label in the window.
  5. $window->show_all() shows the window and all its contents.
  6. Gtk::main() - GTK takes over and starts waiting for events (e.g. mouse or keyboard input).