Multiple Windows

Once you are feeling confident with the situation so far, you can move onto a slightly more complicated script - here's the source code:

    function doshutdown() {

    function btnClick($button) {
        $window =& new GtkWindow();
        $window->set_title("Spawning Windows 2");
        $window->set_default_size(300, 100);
        $label =& new GtkLabel("This is a new window.");
        return false;

    $window =& new GtkWindow();
    $window->set_title("Spawning Windows 1");
    $window->set_default_size(300, 100);
    $window->connect("destroy", "doshutdown");

    $button =& new GtkButton("Click Here");
    $button->connect("clicked", "btnClick");


You should recognise about half of the code from the previous script. For now, again, ignore the two functions near the top and concentrate on the main body of code.

We create our GtkWindow in the same way as last time, however this time we follow up with three new methods: set_title(), set_default_size(), and set_border_width(). These methods are all named quite clearly, but just to make sure we're on exactly the same wavelength, set_title() sets the title bar caption for this window, set_default_size() sets the initial width and height of this window, and set_border_width() sets the amount of margin space in pixels on each edge of the window that is unavailable to child widgets. If set_border_width() had been used with a positive value in the prior example, the GtkButton being used would not have taken up all the space in the window, just what was left after the border.

For all intents and purposes, the rest of the script is pretty much similar, with the most notable exception being the function btnClick(). This time the function is much longer, and also has a return value. If your signal handlers send back false as their return value, PHP-GTK fires the default signal handler as soon as your function finishes, whereas if you return true, it is assumed that you wish no more processing to take place for this signal beyond any other signal handlers you have defined.

At the start of btnClick(), a new GtkWindow is created with a new caption and a default size. Also, a new GtkLabel is created, which is a basic widget that allows you to display short amounts of text. As with GtkButtons, GtkLabels take the string they should display as a parameter when being created, and you can change this string at a later date by using the method set_text().

Continuing on in the function, the label is added to the window, our shutdown function is connected to the "destroy" signal, and the new window is shown. Attaching doshutdown() to the destroy signal of each window being created means that if the user closes any window, the application will terminate - you may want a different situation in your own programs.

The other change in this script is the call to the set_relief() method of our GtkButton. Like the set_title(), set_default_size(), and set_border_width() calls in this script, this method is not necessary , but I have included it to show you more of the GTK functionality. This method takes one of three special constants: GTK_RELIEF_NORMAL (the default setting), GTK_RELIEF_HALF (much lighter shading for buttons), and GTK_RELIEF_NORMAL (no shading for buttons unless mouse is over).

Now you understand what is going on in the script, go ahead and save it as gtk2.php and run it.


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: Handling popup menus >>

Previous chapter: A Basic GUI

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.