+ Reply to Thread
Results 1 to 7 of 7

Thread: Beginning PHP-GTK: Creating a Simple Interface [Tutorial]

  1. #1
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Beginning PHP-GTK: Creating a Simple Interface [Tutorial]


    This tutorial will guide you through creating a simple PHP-GTK Graphical User Interface (GUI). The tutorial assumes that you already know PHP5 and will not cover beginning PHP or programming development. This tutorial is basic but assumes you already know basic PHP-GTK development.

    You should have PHP-GTK installed and able to use it. I suggest using Gnope. Gnope provides an easy method to install PHP-GTK on Windows, Linux and Mac. Gnope is also a method to distribute PHP applications that you complete.

    At the end of this tutorial we will have created an interface that uses tables (GtkTable), Labels (GtkLabel), allows user input (GtkEntry), and two buttons (GtkButton). These widgets will not be connected to any events in this part of the tutorial.

    This tutorial is part of a series. At the end of the tutorial you will have created a very simplistic Keep Alive script. In time, the Keep Alive script will also be a community project and given away for free.

    Getting Started
    Lets begin extending the GtkWindow class and creating a basic GUI to work with:

    Code:
    <?php

    class CodeCallGtk extends GtkWindow {
        
        
    /**
         * FirstGTK constructor. Actually creates the
         * window
         *
         * @return FirstGtk
         */
        
    public function __construct() {
            
    /*
             * Call the parent constructor
             */
            
    parent::__construct();
            
            
    /*
             * Set the size of the new GTK Window
             * Set the Title
             * Connect the destroy/exit event to a function
             */
            
    $this->set_default_size(400,100);
            
    $this->set_title('CodeCall.net PHP-GTK GUI Tutorial');
            
    $this->connect_simple('destroy', array('gtk''main_quit'));

             
    /*
             * This will realize and show
             * all windows from child to parent.
             */
            
    $this->show_all();
        }
    }
        
    new 
    CodeCallGtk();
    Gtk::main();
    Which will result in a window that looks like this:


    Which is empty. The only thing that can be done is minimize, maximize and exit. We need to add contents.

    GtkTable
    All widgets (objects such as buttons, lists, views, etc) must be in a container. There are numerous containers. Three popular containers are GtkBoxes, GtkFixed and GtkTable.

    All windows must have at least one container. A container allows widgets to be attached which are then realized (or shown) in the Window. They can be realized individually or by calling the parent show_all() function (done in the constructor above) which will also realize all child objects.

    GtkTable is a widget that consists of columns and rows. It is the same concept as a table in HTML or a table in a database. The PHP Manual explains it as this:

    GtkTable is a container that provides a simple way of aligning many widgets in a window, both horizontally and vertically.
    Constructor:
    Code:
    GtkTable ([int n_rows [, int n_columns [, bool homogeneous false]]]); 
    rows : The number of rows the new table should have.
    columns : The number of columns the new table should have.
    homogeneous : If set to TRUE, all table cells are resized to the size of the cell containing the largest widget.
    Returns : A pointer to the the newly created table widget.

    We want our program to look like this:


    So we will need 2 rows and 4 columns. This will be how we construct the table:

    Code:
    $table = new GtkTable(4,2); 
    I realize that the image looks like there is only 3 columns and 2 rows but if you setup your table as such, your buttons will be irregular. You could use a button container but that is beyond the scope of this tutorial. For now, just use 4.

    Add this code before the show_all() function is executed:

    Code:
    /*
     * Create a virticle and horizontal box
     * container to hold our widgets
     */
    $table = new GtkTable(4,2);

    /*
     * Add the table to the GTK Window
     */
    $this->add($table); 
    We've created the table and added it to the Window. The table automatically takes up all space available. If you execute the program at this point you will not see any difference. The table is invisible. We still need to attach widgets to the table, but first we need to create those widgets.

    We will come back to the table...

    GtkLabel
    A label is a widget that displays text. They are commonly used to identify objects or provide text updates. We will use a label to identify a user entry, GtkEntry, which accepts a host name.

    Constructor:
    Code:
    GtkLabel ([string string null [, boolean parse_mnemonic false]]); 
    It takes two arguments. The first is the text you would like to display. The second is a boolean value to tell the label to parse mnemonics. A mnemonic is an underlined key in a label, used for keyboard navigation. Both arguments can be null. Creating a label is simple:

    Code:
    $lblHost1   = new GtkLabel(); // Null
    $lblHost2   = new GtkLabel("Host:"); // Text only
    $lblHost3   = new GtkLabel("_Host:"true); 
    Here is an example label with a mnemonic:



    Since we won't be using a mnemonic (yet), we simply need to past text to the Label:

    Code:
    /*
     * Create Label
     */
    $lblHost   = new GtkLabel("Host:"); 
    Add this code before you add the table to the window ($this->add($table)). The placement isn't particularly important, so you could add this after - anywhere before the show_all() function is executed.

    Note: GtkLabel has many methods not covered here. Click Here to see a listing of methods.


    GtkEntry
    GtkEntry is a single line text entry widget, similar to a single line textbox. This widget allows your application to accept user input.

    Constructor:
    Code:
    GtkEntry ([ string text [, int max ]]); 
    text: Default text to be shown
    max: Maximum input length

    Create a new instance of GtkEntry for our program:

    Code:
    /*
     * Create an entry for entering 
     * host to access
     */
    $txtEntry = new GtkEntry(); 
    Add this below your GtkLabel() creation.

    Note: There are several methods and none are discuss here. You can learn about them here as well as some of them in part 2 of this tutorial.

    GtkButton
    Finally, we arrive at the button. The GtkButton widget displays a clickable pushbutton to the user in your container on your window. Upon click, an event is fired.

    Constructors:
    Code:
    GtkButton (string labelboolean use_underline); 
    Code:
    GtkButton::new_from_stock (string stock_id); 
    use_underline is the same as mnemonics above. A boolean value passed that identifies letters with underline values. Why it has a different name? I don't know.

    A stock button is a button that has an image and mnemonic. It is a prefabricated button. You can find a list of stock_ids here.

    Example Stock Button:
    Code:
    $btnStop  GtkButton::new_from_stock(Gtk::STOCK_STOP); 
    Our program will not use mnemonics or stock buttons (yet). We will need two buttons, a start and stop:

    Code:
    /*
     * Create two buttons, one for starting
     * and another for stopping
     */
    $btnStart = new GtkButton('Start');
    $btnStop  = new GtkButton('Stop'); 
    Add this code after creating the GtkEntry() object.

    Note: Again, there are many methods not covered here. You can find a listing here.

    Table Attaching and Display
    At this point you've created several objects but the window doesn't show anything if you execute the code. This is because you have to attach the widgets to the table, which is attached to the window. Because they are not attached to any container, the objects are never realized (nor can be).

    Using the table attach method, we will not add our widgets.

    Constructor:
    Code:
    void attach(GtkWidget childint left_attachint right_attachint top_attachint bottom_attach [, GtkAttachOptions xoptions Gtk::EXPAND|Gtk::FILL [, GtkAttachOptions yoptions Gtk::EXPAND|Gtk::FILL [, int xpadding Gtk::EXPAND|Gtk::FILL [, int ypadding Gtk::EXPAND|Gtk::FILL]]]]); 
    The attach method takes up to 9 arguments of which only 5 are required. We will only be using the 5 required for this tutorial. You can read more about the other options here.

    child: The widget to add.
    left_attach: the column number to attach the left side of a child widget to.
    right_attach: the column number to attach the right side of a child widget to.
    top_attach: the row number to attach the top of a child widget to.
    bottom_attach: the row number to attach the bottom of a child widget to.
    xoptions: Used to specify the properties of the child widget when the table is resized.
    yoptions: The same as xoptions, except this field determines behaviour of vertical resizing.
    xpadding: An integer value specifying the padding on the left and right of the widget being added to the table.
    ypadding: The amount of padding above and below the child widget.

    When attaching a widget, the left_attach and right_attach are the columns while the top_attach and bottom_attach are the rows. Starting with 0, 0-1 is column1/row1, 1-2 is column2/row2, 2-3 is column3/row3, etc.

    To attach a widget to column 2, row 1 you would use this combination
    Code:
    1, 2, 0, 1
    Let's attach our Widgets
    Code:
    /*
     * Attach widgets to the table
     */
    $table->attach($lblHost0101); // Column 1, Row 1
    $table->attach($txtEntry1401); // Comumn 2-4, Row 1
    $table->attach($btnStop2312); // Column 3, Row 2
    $table->attach($btnStart3412); // Column 4, Row 2 
    Since we only use the default options for arguments 6-9, widgets are set to automatically expand/fill the entire space given by the table.

    Add the code above after the button creation and before the $this->add($table);.

    Your code should look like this:
    Code:
    <?php

    class CodeCallGtk extends GtkWindow {
        
        
    /**
         * FirstGTK constructor. Actually creates the
         * window
         *
         * @return FirstGtk
         */
        
    public function __construct() {
            
    /*
             * Call the parent constructor
             */
            
    parent::__construct();
            
            
    /*
             * Set the size of the new GTK Window
             * Set the Title
             * Connect the destroy/exit event to a function
             */
            
    $this->set_default_size(400,100);
            
    $this->set_title('CodeCall.net PHP-GTK GUI Tutorial');
            
    $this->connect_simple('destroy', array('gtk''main_quit'));
            
            
    /*
             * Create a virticle and horizontal box
             * container to hold our widgets
             */
            
    $table = new GtkTable(4,2);
            
            
    /*
             * Create Label
             */
            
    $lblHost   = new GtkLabel("Host:");
            
            
    /*
             * Create an entry for entering 
             * host to access
             */
            
    $txtEntry = new GtkEntry(); 
            
            
    /*
             * Create two buttons, one for starting
             * and another for stopping
             */
            
    $btnStart = new GtkButton('Start');
            
    $btnStop  = new GtkButton('Stop');
            
            
    /*
             * Attach widgets to the table
             */
            
    $table->attach($lblHost0101); // Column 1, Row 1
            
    $table->attach($txtEntry1401); // Comumn 2-4, Row 1
            
    $table->attach($btnStop2312); // Column 3, Row 2
            
    $table->attach($btnStart3412); // Column 4, Row 2
            
            /*
              * Add the table to the GTK Window
              */
            
    $this->add($table);


             
    /*
             * This will realize and show
             * all windows from child to parent.
             */
            
    $this->show_all();
        }
    }
        
        new 
    CodeCallGtk();
        
    Gtk::main();
    Execute the code and you should see this window:


    Part 1 Conclusion
    We've created a very basic GUI. It currently does nothing. The next part of this series we will connect the button click events to functions.

    Continue to Part 2: Signals >>
    ------
    Attached Thumbnails Beginning PHP-GTK: Creating a Simple Interface [Tutorial]-php-gtk1.jpg   Beginning PHP-GTK: Creating a Simple Interface [Tutorial]-php-gtk2.jpg   Beginning PHP-GTK: Creating a Simple Interface [Tutorial]-mnemonic.png   Beginning PHP-GTK: Creating a Simple Interface [Tutorial]-php-gtk3.jpg  
    Last edited by Jordan; 07-14-2009 at 09:36 AM.

  2. #2
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

    Re: Beginning PHP-GTK: Creating a Simple Interface

    Why do you check $GLOBALS['framework']? What is that variable used for?

    What do the two buttons to the left of the minimize button do?

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Beginning PHP-GTK: Creating a Simple Interface

    Quote Originally Posted by John View Post
    Why do you check $GLOBALS['framework']? What is that variable used for?
    $GLOBALS is an array of variables currently in scope. The intended purpose of this variable is to prevent Gtk::Main() from being loaded by other included files. This is actually from the demos code and they set the variable to true in the main files, which loads all of the other demos.

    Since I started with those tutorials early on, that little bit of code has been following me around in my code. It isn't really needed in this example so I'll remove it as not to confuse people.

    Quote Originally Posted by John View Post
    What do the two buttons to the left of the minimize button do?
    Those are from a program called ultramon. It adds extra buttons to allow the user to easily move programs between screens on a dual-screen system.

  4. #4
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,182
    Blog Entries
    12

    Re: Beginning PHP-GTK: Creating a Simple Interface

    im so amazed, ive never seen anything like that before.
    awesome tutorial, +rep for jordan
    *printing*

  5. #5
    Code Warrior BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch's Avatar
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Age
    19
    Posts
    2,223
    Blog Entries
    8

    Re: Beginning PHP-GTK: Creating a Simple Interface

    Dangit now I gotta play around with it now

  6. #6
    Ack
    Ack is offline
    Newbie Ack is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    1

    Re: Beginning PHP-GTK: Creating a Simple Interface [Tutorial]

    Thanks for the tutorial, i've been trying to do this stuff for a little bit and yours really helped.

  7. #7
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Beginning PHP-GTK: Creating a Simple Interface [Tutorial]

    Sure, no problem. Check out the second part as well: Beginning PHP-GTK: Signals

    I'll hopefully have the 3rd part finished sometime soon.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. how update an old php script
    By midnightrose2008 in forum PHP Forum
    Replies: 1
    Last Post: 07-07-2008, 09:39 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts