1.1 Hello World!
Written by kksou   
Wednesday, 12 March 2008

Objective

Let’s get started learning php-gtk2 with this simple yet complete "hello world!" script.

We will also understand the four key lines that constitute a standard php-gtk2 script. You will see these four lines in almost all the sample codes in this book.

Overview


Sample Output

1.1.gif

Sample Code

1   
2   
3   
4   
5   
6   
7   
<?php
$window = new GtkWindow(); // note 1
$window->connect_simple('destroy',array('Gtk','main_quit')); // note 2

// add your widgets here
$label = new GtkLabel("hello world!");
$window->add($label);
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.

Explanation

For almost all php-gtk2 scripts, you will find the following four key lines (highlighted in blue):

  1. Creates a new window.
  2. Ensures a clean exit when you close the window. This basically says call Gtk::main_quit() when the user close the window.
  3. Displays all widgets that you have created, in this case, the window and the label.
  4. Let GTK take over and start waiting for events (e.g. mouse or keyboard input).

Note

  • These four lines form the simplest yet complete php-gtk2 script. For this “hello world” example, we simply stuff two more lines of code between these 4 lines:
  • $label = new GtkLabel("hello world!");
    $window->add($label);
    

    The first to create a new label, and the second to stuff the label inside the window.

  • You can also combine these two lines into one:
  • $window->add(new GtkLabel("hello world!"));
    


User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved