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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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

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

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

//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

$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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap

Dev Shed Tutorial Topics: