HomePHP Page 6 - Programming with PHP and GTK, Part 1
A Simple Program - PHP
Have you ever thought of writing a PHP application for client side execution without having a web server present? Learn the basics of creating a front end for PHP using PHP-GTK.
Now that we know how to create windows and widgets in PHP-GTK, let's go on to make a simple program. Let's put these all together in a program which will allow you to enter a string in a Entry field, when the Button "UpperCase" is clicked, the string will be converted to uppercase and displayed back in the Entry field and when the button "LowerCase" is clocked, the string will be converted to lowercase and displayed.
<?php
if (!extension_loaded('gtk')) { dl( 'php_gtk.' . PHP_SHLIB_SUFFIX); }
function UCText($theButton, $theEntry) { $text = strtoupper($theEntry->get_text()); $theEntry->set_text($text); }
function LCText($theButton, $theEntry) { $text = strtolower($theEntry->get_text()); $theEntry->set_text($text); }
function destroy() { gtk::main_quit(); }
// Create a new window $window = &new GtkWindow(); $window->set_title("Play with Strings"); $window->set_usize(300, 60); $window->set_border_width(3); $window->set_position(GTK_WIN_POS_CENTER); // Set a callback function for the destroy signal $window->connect("destroy", "destroy");
// Add a GtkVBox class to our window $box = &new GtkVBox(); $window->add($box);
// Add a GtkEntry class $entry = &new GtkEntry(); $entry->set_text("Play with Strings"); $box->pack_start($entry);
//Tooltip for Uppercase Button $tt1 = &new GtkTooltips(); $tt1->set_delay(200); $tt1->set_tip($button1, 'Click here to Convert to Uppercase', ''); $tt1->enable();
//Tooltip for Lowercase Button $tt2 = &new GtkTooltips(); $tt2->set_delay(200); $tt2->set_tip($button2, 'Click here to Convert to Lowercase', ''); $tt2->enable();
// Show the window $window->show_all();
// Start the main PHP-GTK listener loop gtk::main();
?>
You'll notice I've added some extra calls while creating the window. Here's what they are:
* $window->set_border_width(3) : This sets the window border to 3. This is to ensure that the widgets to expand to the fill size of the window. * $window->set_position(GTK_WIN_POS_CENTER) : This sets the window position to the center of the screen.
Let's analyze the function UCText.
function UCText($theButton, $theEntry) { $text = strtoupper($theEntry->get_text()); $theEntry->set_text($text); }
This when this function is called, the instance of the Entry Widget is passed to this function. This allows us to access the text in the Entry Widget. The text is converted to uppercase using the PHP function strtoupper, and the text is stored back into the Entry Widget. The function LCText also functions the same way. Now how did I pass the instance if the Entry Widget to this function? If you look at the callback function for the button clicked signal, I've added $entry to the connect function to pass the instance if $entry to these functions
Now let's save and run this program to see how it works. As soon as you run this program, you'll get a window quite similar to this:
Click on the Uppercase and LowerCase buttons to see the text in the Entry Widget change. You can also edit the string in the Entry Widget and click on the buttons to see your text change.
Now that we've covered the basics of creating a front end for PHP using PHP-GTK, you can experiment with this and start creating stand-alone PHP applications for Windows and Linux. In the next part of this series I'll be showing you how to create database applications using PHP-GTK.