spacer
Web Development Tutorials PHP Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Desktop Application Development with PHP-GTK

By Akash Mehta
2008-03-13


Your first PHP-GTK application

Load up your favourite text editor and bash out the following:

<?php
$wndMain = new GtkWindow();
$wndMain->connect_simple('destroy', array('gtk', 'main_quit'));

$lblHello = new GtkLabel("Hello World!\r\nBuilt using my very first PHP-GTK script!");
$wndMain->add($lblHello);

$wndMain->show_all();
Gtk::main();

Save the file as "helloworld.php", either in C:\Gnope\ (Windows) or your home directory (Linux). (This is merely for consistency throughout this tutorial; It doesn't actually matter where you save the file.). Open up a command prompt / terminal (Start > Run > "cmd" on Windows) and switch to the directory where you saved the file - "cd C:\Gnope\" (Windows) or "cd ~" (Linux). Execute "php helloworld.php" and you should see something like this:

It isn't terribly attractive, but it isn't too bad for six lines of code either.

(If you get a fatal error, "call to undefined function connect_simple()", you're using PHP-GTK 1. Grab the binaries for / compile from source PHP-GTK 2 and try again.)

Play around with the window a little bit. Try resizing it:

So, what's going on here? Let's take a look back at the code:

$wndMain = new GtkWindow();
$wndMain->connect_simple('destroy', array('gtk', 'main_quit'));

First, this section creates a new GTK window. The GTK toolkit is comprised of a variety of different controls, from text boxes to drop down menus and just basic text labels. A window itself is (in a way) one of these controls, and handling it somewhat generically gives us a lot of flexibility. GTK works with containers, inside of which you can place controls, and a window is a special type of container.

Most items have some special events, and GTK calls those events "signals". Just like you can bind to events in Javascript, you can assign callbacks for signals. In this case, the "destroy" signal - called when the user clicks the close button on the window - links to the callback gtk::main_quit(), which will handle garbage collection and allow us to end gracefully. See this documentation page on signals for more details.

$lblHello = new GtkLabel("Hello World!\r\nBuilt using my very first PHP-GTK script!");
$wndMain->add($lblHello);

Now we get to start working with controls. We'll start with a label, a simple non-editable text field. Where in HTML you can output text anywhere and control it via CSS elsewhere, desktop applications are a little more strict.

To work with controls in PHP-GTK, we use classes. When we create a new object of a control class, it's stored in memory but doesn't actually appear anywhere - because GTK doesn't know where we want it to appear. We then need to go back to our window and instruct PHP-GTK to add the label to the window. PHP-GTK takes care of the rest; when we resize the window, it simply centers the label within the container, no messy Javascript or unreliable CSS hacks.

$wndMain->show_all();
Gtk::main();

Finally, we tell GTK to actually display the window - otherwise it just sits in memory. Once we're done, however, we need GTK to take over and just wait. Unlike a web-based script, which continues processing and finishes, to maintain the state of a desktop application window and wait for user input we need some kind of an infinite loop or wait-for-event system. While I won't go into details of internals here, suffice to say a call to Gtk::main()allows PHP-GTK to take over and we launch into our application.



Tutorial Pages:
» What is PHP-GTK?
» Getting started with PHP-GTK
» Your first PHP-GTK application
» So how powerful is this PHP-GTK?
» Building a real application in PHP-GTK
» Further reading


Related Tutorials:
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Installing PHP on Windows
» Easy PDF Generation in PHP



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: