Home arrow PHP arrow 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.

  1. Programming with PHP and GTK, Part 1
  2. Installing PHP-GTK
  3. A Test Run
  4. A PHP-GTK Window
  5. Widgets
  6. A Simple Program
By: Vinu Thomas
Rating: starstarstarstarstar / 40
August 16, 2004

print this article



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.


if (!extension_loaded('gtk')) {
dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);

function UCText($theButton, $theEntry)
   $text = strtoupper($theEntry->get_text());

function LCText($theButton, $theEntry)
   $text = strtolower($theEntry->get_text());

function destroy()

// Create a new window
$window = &new GtkWindow();
$window->set_title("Play with Strings");
$window->set_usize(300, 60);
// Set a callback function for the destroy signal
$window->connect("destroy", "destroy");

// Add a GtkVBox class to our window
$box = &new GtkVBox();

// Add a GtkEntry class
$entry = &new GtkEntry();
$entry->set_text("Play with Strings");

// Add UpperCase GtkButton
$button1 = &new GtkButton("UpperCase");
$button1->connect("clicked", "UCText", $entry);

// Add LowerCase GtkButton
$button2 = &new GtkButton("LowerCase");
$button2->connect("clicked", "LCText", $entry);

//Tooltip for Uppercase Button
$tt1 = &new GtkTooltips();
$tt1->set_tip($button1, 'Click here to Convert to Uppercase', '');

//Tooltip for Lowercase Button
$tt2 = &new GtkTooltips();
$tt2->set_tip($button2, 'Click here to Convert to Lowercase', '');

// Show the window

// Start the main PHP-GTK listener loop


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());

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

$button1->connect("clicked", "UCText", $entry);
$button2->connect("clicked", "LCText", $entry);

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:

PHP and GTK+

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.

>>> More PHP Articles          >>> More By Vinu Thomas

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: