Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Develop Desktop GUI Apps with PHP-GTK, the Standalone PHP : Page 4

PHP enters the standalone GUI application arena with PHP-GTK. Learn the basic building blocks of PHP-GTK coding with a tutorial for first installing the language and then building and connecting its widgets, buttons, and functions.


advertisement
You're Ready to Code Desktop Apps
Now that you have PHP-GTK running, you are ready to create standalone GUI applications. PHP-GTK contains a wealth of widgets and buttons, and anything you can do with PHP, you also can do with PHP-GTK. As an example, start by building a simple PHP-GTK widget window.

Comments in PHP-GTK are the same as in PHP or C:

/* insert C-style comments here */

The first thing to know about PHP-GTK is that it opens and closes the same way other PHP code does. The first line of code is always <?php, and the last line is ?>.



Second, you must detect the operating system (OS). As such, the following code snippet is necessary for all PHP-GTK applications:

<?php if (!class_exists('gtk')) { dl('php_gtk.' . (strstr(PHP_OS, 'WIN') ? 'dll' : 'so')); } /*Now, create the window widget as a variable.*/ $window = &new GtkWindow(); /*Next, create a title for this new window widget.*/ $window->set_title("PHP Window Number 1"); /*Then you will set the widget window to the center of the screen.*/ $window->set_position(GTK_WIN_POS_CENTER); /*This statement is also necessary in all PHP-GTK applications that have graphics on the screen. This shows all the parent widgets and their children.*/ $window->show_all(); /* Run the program from top to bottom. */ Gtk::main(); ?>

This creates a simple window widget (see Figure 10). Try it out. Simply paste the code into Notepad or a similar text editor and save the file as phpwindow1.php.

Figure 10. PHP-GTK Window Widget: Test out the PHP-GTK window widget.

To make this example a little more exciting, add a button to the window with the following code:

$button = &new GtkButton('clickme’); $window->add($button);

The first line creates the button variable; the second line makes the button a child of the Window widget; and you've named the button clickme.

Now, you can move on to the fun part: adding an event. It is one thing to have a button, and quite another to have that button perform a function. So the next step is to associate an event with your button. Generally, when you create a button, you use three lines of code. Typically, the event goes between the two lines you added above:

$button = &new GtkButton(‘clickme’); $button->connect('clicked', 'destroy'); $window->add($button);

The second line associates the 'clicked' action with the destroy function (which you will create shortly). When you click the button (which you'll name open_sesame) now, you are calling the destroy function:

/* function to stop the PHP-GTK application as a process */ function destroy() { gtk::main_quit(); }

The destroy function causes the gtk::main_quit(); action to occur, closing the PHP-GTK application you created. When you try this out, your complete new code should look like this:

<?php if (!class_exists('gtk')) { dl('php_gtk.' . (strstr(PHP_OS, 'WIN') ? 'dll' : 'so')); } /*Now, create the window widget as a variable.*/ $window = &new GtkWindow(); /*Next, create a title for this new window widget.*/ $window->set_title("PHP Window Number 1"); /*Then we will set the widget window to the center of the screen.*/ $window->set_position(GTK_WIN_POS_CENTER); $button = &new GtkButton('clickme'); $button->connect('clicked', 'destroy'); $window->add($button); function destroy() { gtk::main_quit(); } /*This statement is also necessary in all PHP-GTK applications that have graphics on the screen. This shows all the parent widgets and their children.*/ $window->show_all(); /* Run the program from top to bottom. */ Gtk::main(); ?>

Save this file as makemebreakme.php on your desktop. If you followed the file association instructions during the PHP-GTK install, all you need to do is double click on this file to activate it. Once the PHP application window and button appear, simply clicking the clickme button will call the destroy function, closing the application. These are the basic building blocks for all PHP-GTK coding.

Now—to take this example up a notch—alter this code slightly to link the clickme button with a much more powerful function and widget set. Again, you will use the basic building blocks above. Only this time, the destroy function is called from within another function called openfiles (see Listing 1).

Cut and paste the code in Listing 1 and save it as opensesame.php. Double click on the application, and then click on the clickme button (see Figure 11). This should call the openfiles function, allowing you to browse the directory tree of your computer (see Figure 12).


Figure 11. PHP Window Number 1: Pushing the clickme button to call the openfiles function.
 
Figure 12. Open Sesame Accesses Directory Tree: Calling the GtkFileSelection widget to browse the directory tree.

Now you are using PHP-GTK to talk directly to your file structure, where you can create or delete files. This is just the beginning of the possibilities this language offers. Java and Visual Basic coders, take notice!

I hope these instructions have set you on your way to coding some great desktop applications with the PHP-GTK language. The only limit is your imagination.

Thanks to Tim Schneider for the Dreamweaver fix.



Gregory L. Magnusson is the founder of Cyborg Spiders Web Development. Since experiencing X-Windows with FreeBSD in 1999, he hasn't looked back. In the current horse race of developing Web technologies, Greg is putting his money on PHP.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap