Hello World
A simple yet complete 'first-time' GTK application.
Topic: GtkWindow (View All Tutorials)
Keywords: gtkwindow gtklabel
Updated: 280 Days Ago, 2007/11/14 21:17
Our First Window
Here will recreate the infamous Hello World example that has been the first project of programmers ever since creamy peanut butter. Maybe even a little before.

When we create a GUI application, this is basically the logic flow we step through to create everything and set it into motion.
- Define our GUI Elements (windows, buttons, etc).
We define our classes and functions which are centered around creating the user interface.
- Define our action functionality.
GUI toolkits like GTK are event based, so we create functions which perform actions when the user does something. For example, if we have a button, we create a function to run when that button is clicked and therefore perform the action requested.
- Trip an event to set everything into motion.
Since things are event based here, we have to set off an event which will cause a chain reaction. Most of the time this is creating the first window automatically. The chain reaction is the user interacts with this window which then performs more actions later.
The Bare Minimums
For a successful and clean application there is a bare minimum of functionality it must have.
- It must be able to initalize itself.
- It must be able to terminate itself.
To initalize, the application defines and creates the environment. To terminate, the application must know how to end itself cleanly. The first is simple, but the second takes a moment of thought. How do we terminate an application, purely from a GUI point of view? (ignoring that we could just hit CTRL+C on the command line interface which launched it)
We need some sort of Quit action to terminate the application. This could be done on a menu system such as File > Quit - but also by clicking the X on the window title bar. As a bare minium we need to tell the application how to quit when the user clicks their X.
Hello World
#!/usr/bin/php -c/etc/gtk/php.ini
<?php
//. note 1
class bobWindow extends GtkWindow {
public $label;
public function __construct() {
parent::__construct(); // note 2.
$this->label = new GtkLabel('Hello World!'); // note 3
// note 4.
$this->set_size_request(300,200);
$this->set_title('Hello World!');
$this->connect_simple('delete-event',array($this,'on_quit'));
// note 5.
$this->add($this->label);
$this->show_all();
return;
}
public function on_quit() {
// note 6.
$this->hide();
Gtk::main_quit();
return;
}
}
// note 7
$window = new bobWindow;
// note 8
Gtk::main();
// note 9
$window->destroy();
unset($window);
?>
Note 1
The first thing I am going to do is extend the default GtkWindow class to setup our default application. Doing this bobWindow gains all the functionality of GtkWindow and then we will add more.
Note 2
Next, I run the parent constructor (GtkWindow's constructor) so that my class gets all the default attributes of the parent class (again, GtkWindow).
Note 3
bobWindow has a public property called label, which I am going to use to store the GtkLabel object. The GtkLabel class can take an argument on the creation of a new object which I have used to set the text on it to "Hello World".
Note 4
Using methods that were given to bobWindow by GtkWindow, I set the default size (and minimum) size of the window to 300 pixels wide by 200 pixels tall. Then I set the title bar text to "Hello World".
The next line is important, the connect_simple() method is the one used to connect signals to functions. A signal is a type of event which triggers the application to run a function. The delete-event signal is the signal for when the window is closed, such as by means of clicking the X on the title bar. Notice how to tell it which function to use? It is to be given an array with the first element being a reference to an object, in this
case this object, $this, and the second element is a string, the name of the method in this object to execute.
Bare Minimums
This makes our application complete the bare minimum rules I mentioned earlier.
Note 5
Windows can only hold one item at a time, and in this example there is only one item (the GtkLabel) so this is O.K. I add() the GtkLabel to the window, and then show_all() on the window. show_all() will tell the window to become visible, as well as all the items we have packed inside it.
Note 6
This is the function that is run when the program is terminated. It hides the window and then tells GTK to quit the main loop. (that will make more sense after Note 8)
Note 7
This is actually the first line of executing code in the entire program. Up till now, everything has been just definition code - the defining of our GUI and actions. I trip the series of events by creating a new instance of bobWindow which as we have already seen is a self constructing window.
Note 8
Begin the GTK main loop. Literally, this is where the program will be 'stuck' throughout the entire running cycle of the program. Once it gets here it remains here until the main loop is quit. Remember the on_quit() method?
Note 9
Once the main loop is forced to quit, the program proceeds where it left off. Everything after the main loop in this application is all cleanup code.
When you are done with a Gtk Object, you should free it so that any resources it was using can be given to something else. Most Gtk Objects have the destroy() method available. This will cause GTK to delete all the objects where were packed into it, and then delete the object iself. Then you can unset() it.
Running This Program
So I save this as hello-os.phpg. If the system is configured correctly then on an OS like Linux we can give this file execute permissions and execute it in place...
chmod 755 hello-os.phpg
./hello-os.phpg
In Windows, it could be as easy as
double-clicking.
End Game