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.
Glade gives you 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.
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.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Using Custom Parameters >>
Previous chapter: Handling popup menus
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.