Hudzilla.org - the homepage of Paul Hudson
Contents > Alternative PHP uses > Graphical user interfaces Wish List | Report Bug | About Me ]

21.3.6     Advanced GUIs

This is NOT the latest copy of this book; click here for the latest version.

There are so many possibilities using PHP-GTK that, sadly, I have had to pick and choose what I can cover here owing to space reasons - and that is despite the fact that this tutorial is extra long! So far we've looked at windows, buttons, labels, menus, and menu items.

What we're going to look at now is an easy way to use all sorts of GTK widgets, perfectly lined up where you want them to be, with many widgets in the same window, and, surprisingly enough, with almost no work. This, young grasshopper, is the power of Glade .

Available from http://glade.gnome.org, Glade is a GPLed GTK+ user interface builder designed to allow you to design and build your GUI, including defining signal handler functions, with little work.

Take a look at the screenshot below to see Glade in action.



As you can see in the picture, you have a big toolkit available to you under the "GTK+ Basic", and another large toolkit available under "GTK+ Additional". When you want to make use of a particular widget, you simply have to select from the toolbox and "draw" on your window. Properties can be set from a property editor which is partly off-screen to the left. Once you are finished, you can even instruct Glade to generate source code for you, although sadly, this is not yet available in PHP.

However, there is still a way Glade can be used with PHP. Take a look at this final script:

<?php
    
function doshutdown() {
        
gtk::main_quit();
    }

    
$layout = &new GladeXML('complex_interface.glade');
    
$layout->signal_autoconnect();
    
$window = $layout->get_widget('window1');
    
$window->connect("destroy", "doshutdown");

    
Gtk::main();
?>

Here there is a new class, available if you have libglade installed, that takes the .glade project file that Glade saves for its own purposes and translates that into a GUI. This GUI, stored in $layout in the example above, can then have its signals connected using the GladeXML method signal_autoconnect().

In order to provide a clean shutdown of the script, I have used the GladeXML method get_widget() to grab the main window. Note that you may need to change this line if you have used a particular name for your window in Glade. get_widget() takes just the one parameter, which is the name of the widget you wish to get from the layout, and returns the widget for you to use.

With our GtkWindow reference, I have connected the destroy signal to our usual doshutdown() function, and that is the end of the script.

If you save that as gtk4.php, then go experiment with Glade to see what you can make. You can see my interface live in action in this next screenshot:



As you can see, using Glade takes all the hard work away from designing a GUI. All that is left to do now is to write handlers for all the signals you wish to work with, and your interface is done.





<< 21.3.5 Handling popup menus   21.3.7 Using Custom Parameters >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow