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.

devx-admin

devx-admin

Share the Post:
Performance Camera

iPhone 15: Performance, Camera, Battery

Apple’s highly anticipated iPhone 15 has finally hit the market, sending ripples of excitement across the tech industry. For those considering upgrading to this new

Battery Breakthrough

Electric Vehicle Battery Breakthrough

The prices of lithium-ion batteries have seen a considerable reduction, with the cost per kilowatt-hour dipping under $100 for the first occasion in two years,

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW)

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and

Chip Overcoming

iPhone 15 Pro Max: Overcoming Chip Setbacks

Apple recently faced a significant challenge in the development of a key component for its latest iPhone series, the iPhone 15 Pro Max, which was unveiled just a week ago.

Performance Camera

iPhone 15: Performance, Camera, Battery

Apple’s highly anticipated iPhone 15 has finally hit the market, sending ripples of excitement across the tech industry. For those considering upgrading to this new model, three essential features come

Battery Breakthrough

Electric Vehicle Battery Breakthrough

The prices of lithium-ion batteries have seen a considerable reduction, with the cost per kilowatt-hour dipping under $100 for the first occasion in two years, as reported by energy analytics

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW) of wind, solar, and energy

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and wind sources. This funding will

Renesas Tech Revolution

Revolutionizing India’s Tech Sector with Renesas

Tushar Sharma, a semiconductor engineer at Renesas Electronics, met with Indian Prime Minister Narendra Modi to discuss the company’s support for India’s “Make in India” initiative. This initiative focuses on

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of constructing residential and commercial buildings.

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These