devxlogo

Develop Desktop GUI Apps with PHP-GTK, the Standalone PHP

Develop Desktop GUI Apps with PHP-GTK, the Standalone PHP

HP, an increasingly popular Web scripting language (see Figure 1), is particularly well suited for the creation of dynamic database-connected Web sites. The two strengths that make PHP a powerful server-side scripting language are its abilities to:

  1. embed directly into existing HTML
  2. connect to a wide variety of databases
Figure 1. PHP Usage Statistics, April 2004: PHP is an increasingly popular Web scripting language.

Now, PHP has begun to carve a space for itself in the standalone application arena as well with PHP-GTK. As its name indicates, PHP-GTK combines PHP with the GIMP Tool Kit (GTK), an object-oriented framework for developing graphical user interfaces (GUIs). GIMP (GNU Image Manipulation Program), which was developed using X11 on Linux platforms, basically enables the same PHP-GTK code to run on Linux, MS Windows, BeOS, and Mac 0SX without any changes. So while PHP requires a server?often Apache?to run, you can use PHP-GTK to create standalone GUIs that run on Windows, Linux, or Mac desktops, independently of a server.

This article provides a guide to getting PHP-GTK up and running on your Windows and/or Linux desktop(s), and concludes with a tutorial for building and connecting its widgets, buttons, and functions in a desktop application.

Deploy PHP-GTK on Windows
First, you have to get a hold of the PHP-GTK installer. Create a folder on your C: drive called php_gtk, and download the file “php-gtk-1.0.0 Windows and PHP Binary including ComboButton, Extra, libGlade, Scintilla, Spaned, SQPane – 23-Oct-2003” to it.


Figure 2. Choose Which Program Opens .php Files: Choosing which program opens .php files.
 
Figure 3. Open .php Files with PHP_Win.exe: Open .php files with php_win.exe.

Double click the downloaded file to start your file decompression program (e.g., Winzip or Freezip), and extract its files into the C:php_gtk folder.

Navigate to the extracted files in your C:php_gtk folder. Cut and paste the folder “test” into the folder “php4”. If this is a virgin PHP install for you, cut and paste the folder “php4” to your C: drive to create C:php4. If you have PHP installed on your Windows server, keep your php.ini file inside the php4 folder to avoid conflicts (alternatively, you can upgrade your existing php.ini file with the new php-gtk.ini file).

Figure 4. Always_Use_Win_php_exe: Always use win_php.exe to open .php files.

If you run Windows 95/98/XP, copy the php.ini file (found in C:php_gtkwinnt) to the C:windows directory. If you run Windows 2000 or Windows NT, copy this php.ini file to your existing C:winnt folder.

Finally, you have to get the PHP files to associate with php_win.exe. Navigate to C:php4 esthello.php and right click on it. Select Open With -> Choose Program (see Figure 2)?>Other (see Figure 3), and then associate php_win.exe with your .php files by browsing to C:php4php_win.exe, selecting Open, and checking the Always use this program to open these files box (see Figure 4).

Now, .php extensions will automatically run when they are double clicked, and the desktop icons of existing .php files have changed to the PHP-GTK icon. Navigate to the folder C:php4 est and double click on hello.php. Try them all to see some of the possibilities of PHP-GTK.

One problem Dreamweaver users may face is that Dreamweaver will associate itself with their .php files. To solve this dilemma, open Windows Explorer?>Tools?>Folder Options. For Windows 98 users, this will be Windows Explorer?>View?>Folder Options (see Figure 5).


Figure 5. Explorer_Tools_Folderoptions1: A screenshot of Windows Explorer Folder Options.
 
Figure 6. Scroll_Folderoptions_Advanced2: Scroll folder options to remove Dreamweaver file associations.

Choose File Types and scroll until you find php and php3 (see Figure 6).

Click on the Advanced tab and remove any associations. Then click Okay. Navigate to the C:php4 est folder and check out the sample applications included.Linux Install
I find the CVS Version of PHP-GTK to be the most straightforward for a Linux install. The following steps will guarantee you have the most up-to-date version of PHP-GTK.


Figure 7. Cvstage0: Using the terminal to install PHP-GTK over the Internet.
 
Figure 8. Cvsstage1: Using the terminal to install PHP-GTK over the Internet.

Log into the PHP anonymous CVS server by opening a terminal window.

TYPE: cvs -d :pserver:[email protected]:/repository login (see Figure 7)

TYPE: phpfi at the password prompt and get the PHP-GTK tree.

TYPE: cvs -d :pserver:[email protected]:/repository co -r PHP_GTK_1 php-gtk (see Figure 8)

Figure 9. Buildconf-configure: Building and configuring PHP-GTK on Linux.

Move into the PHP-GTK directory source tree.

TYPE: cd phpTYPE: cd php-gtk

Configure and install PHP-GTK in typical Linux fashion.

TYPE: ./buildconfTYPE: ./configure

Note: Configure has a lot of options. For a more complicated build, try typing ./configure?help first (see Figure 9).

TYPE: make

TYPE: make install

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 .

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

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:

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist