PHP
  Home arrow PHP arrow Page 6 - Programming with PHP and GTK, Part 1
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Programming with PHP and GTK, Part 1
By: Vinu Thomas
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 40
    2004-08-16


    Table of Contents:
  • Programming with PHP and GTK, Part 1
  • Installing PHP-GTK
  • A Test Run
  • A PHP-GTK Window
  • Widgets
  • A Simple Program

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Programming with PHP and GTK, Part 1 - A Simple Program
    ( Page 6 of 6 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT