Introduction and application of PHP-GTK

Source: Internet
Author: User
Tags end execution ftp functions connect net signal handler zend
1. PHP-GTK Introduction

PHP-GTK is a php extension module that allows programmers to write programs that are executed on a client-side, independent GUI. This module does not allow the GTK + program to be viewed in the browser, and it is initially developed to write a stand-alone GUI program.

1.2 GTK
GTK was originally developed for GIMP, a GUI image-processing software. GTK + is a suite of GIMP tools. GTK + has grown from here until it has become the center of GNOME (Gnome is a desktop environment). GTK + has also been promoted to BeOS and Win32, making it the best choice for the PHP extension module, which enables PHP to develop Windows interface programs across platforms and PHP for linux,beos,windows platforms.

2. PHP-GTK Concept

2.1 Preface
And then we're going to teach you a little bit more conceptual stuff. Because the concept of this chapter is very important, so even if you do not understand, you still have to understand it slowly, otherwise, then. What's more, the next thing is not to recommend readers who have no programming experience, because there are a lot of ideas that can easily be confused. Also, the next part of the English I will use English, so that everyone in the reading of foreign documents will not be overwhelmed, come on!! If you have any knowledge of this chapter, please check it yourself
PHP-GTK manual:

2.2 Widget (s)
Widgets are the basic functions and forms in a GUI program. The most commonly used widgets are: Label, button, window, frame, and text box. All the widgets come from an abstract basic class─gtkwidget. Each widget is a class

A widget has about five periods in a lifetime:
1. Establishment (creation): Declaring an object (declaring an object)
2. Place (placement): add it to a container (adding it to a container)
3. Signal connection (Signal Connection): Receive signal and perform action (the action it'll perform)
4. Show (display): Is it visible (whether it is viewable or not)
5. Delete (destruction): Close Program (closing of a)

2.3 Container (s)
Container is a widget that can contain other widgets. Most of the widgets are container, such as: Gtkwindow, Gtktable and Gtkbox. Besides this, container is no different than any other widget, and can be put into other container. And all the container comes from a class─gtkcontainer, and it comes from the class of Gtkwidget. So container is also one of the widgets.

2.4 Signal (s)
When a programmer makes an action in a program, the program needs to have an action to respond to the user's action. Signals enables the program to know that the user has done the action and can trigger the appropriate response.

For example, when a user presses a button that opens a new window (Gtkbutton), the program recognizes the request and opens a new window. This matter can be done through signal. When the button is pressed, the widget emits a signal, then the signal triggers the callbacks, creating a new Window (Gtkwindow).

2.5 Callback (s)
Callback is the function that is aroused by signal when signal is sent out. Callback performs a function to return a value or to do an action. Callback is signal's handler Funciton. It can be a preset handler for the signal or a function defined by the programmer. To build a callback, you must connect the function to the signal.

2.6 Signal Inheritance (inherited)
As with methods, signals can be inherited by objects. A widget can send out any of its parent widgets and its own unique signal.

2.7 Connecting signals
You must specify a callback function for PHP-GTK to respond to signal when signal is sent out. Connecting a signal to a function can be achieved by using the Connect () object method.

As follows:

Build a Gtkwindow
$window = &new Gtkwindow ();
Connect the "destroy" signal with the Connect () method to the Shutdown function
$window->connect ("Destroy", "shutdown");
Create a Gtkbutton, button text for "Press Me"
$button = &new Gtkbutton ("Press Me");
$button->connect ("clicked", "you_clicked");
Put the Gtkbutton in the Gtkwindow of container.
$window->add ($button);
Show $window and all of its child widgets
$window->show_all ();
Enter the main loop of the program (meaning that the program starts)
Gtk::main ();

Execute it, there will be a window, there is a "press me" button, press the button program will execute the you_clicked function. In this program, the "destroy" signal of a $window object is sent when the user presses the "X" in the upper-right corner of the window, and the "clicked" signal of the $button object is sent when the user presses the button. The last line of Gtk::main () must be executed, so as to tell the computer to start executing the program, since there is a start, then there must be a stop it? Yes, with Gtk::main_quit () You can stop the program.

Having read the above examples, some readers may have questions about what to do if I want to perform a widget that is outside of the signal widget. "At this point, you need to use another method for a connect_object (), It can call methods across objects or pass other objects as a function of three. Cross-object call methods are as follows:

$window->connect_object ("Destroy", Array ("GTK", "Main_quit"))

Thus, the method of Gtk::main_quit () is evoked when the "destroy" signal of the $window object is sent out, and the program will eventually refer to execution.

In the end of the introduction to the connection method, again mention the custom increase of connect () and Connect_object () to pass to the callback function of the three methods. See Example:
$parameter = "New Superman";
$button 1 = &new Gtkbutton ("test");
Connect the "clicked" signal to the Who_are_you function, with the addition of three $parameter
$button 1->connect ("clicked", "Who_are_you", $parameter);
$button 2 = &new Gtkbutton ("Test II");
Connect the "clicked" signal to the Kill_the_button1 function, with the addition of three $button1
$button 2->connect_object ("clicked", "Kill_the_button1", $button 1);

function Who_are_you ($widget, $parameter) {
Echo $parameter;

function Kill_the_button ($button) {
$button->destroy ();

Notice that the two function,who_are_you are two three, right? What's the first one for? Why does it automatically appear?? Because, each signal callback function will be signal different and some of the default must be passed into the callback function of the three, and basically all signal will at least pass to callback Function A three-number a produces the object of the signal. So Who_are_you's first three numbers are $button1, and the second one is $parameter, the new Superman. The Kill_the_button function is different. ROM ~ because the Connect_object () function calls up the preset three of the callback functions that were originally signal, so Kill_the_button is only attached to connect_ The last $button1 of object, so that Kill_the_button can call the $button1 method or obtain its properties, here call the $button1 destroy method, so $button1 will be eliminated.

2.8 Event (s)
Event is a kind of signal, but its use is also very powerful. As far as signal is concerned, signal is built on widgets, so, for example, Gtkwindow has no "clicked" signal, so without the event signal, Gtkwindow is the most unlikely to send out clicked and the like signal. What if you use the event signal? Event signal is allowed to be added to any widget, so even if the widget does not have the function of "clicked" signal, you can use Add_events () to add it to the event What kind of reaction signal will do. The event signal contains more information, for example, when you use the "key-press-event" event signal, you will also record what button you pressed, which is usually the event signal callback The function format will have two three, the first is still send out the signal widget, and the second is $event, this $event is a class, the attributes and methods in which will be sent over the event signal different kinds. On the "Key-press-event" returned $event class, there is a property is Keyval, content is the user pressed which key. These are often useful information for a programming designer. So the importance of event can not be ignored, even if the beginning will be a little bit do not understand, but also slowly into the only line. This section is also very important.

3. Install PHP-GTK

3.1 Installing under Windows system
First to download from the ... HP-GTK Windows binary file (0.5.1) At the time this article was written.

Then look at the contents of the PHP-GTK 0.5.1 binary file:
php4→php and PHP-GTK Binary archives
winnt→ preset php.ini files
WINNTSYSTEM32→GTK binaries used by extension
test→ several test files.
readme.txt→ Installation Specification File

To begin the installation:
1. Copy the contents of PHP4 to your PHP installation directory (example c:php).
2. Copy the contents of Winnt to your Winnt folder. On Windows NT or Windows2000 is C:winnt, c:windows on Window95, 98, and XP. If there is already a php.ini in the folder, do not do this action.
3. Copy the contents of Winntsystem32 to your Winntsystem32 folder. On Windows NT or Windows2000 is C:winntsystem32, c:windowssystem32 on Window95, 98, and XP.
4. Copy the contents of test to where you want to execute your script (this step is not necessary).

How to execute a PHP-GTK program:
The PHP-GTK program can be started by entering instructions (or creating shortcuts) under "Start"-"execution", such as: C:phpphp-q c:phptestgtk.php # means to not print out HTTP headers, but always use this window until you close the program.
C:phpphp-q-C php.ini c:gtk.php # Ditto, but executes the specified php.ini settings.
c:phpphp c:phptestgtk.php # # indicates that the HTTP Header will be printed, but has been
Use this window until you close the program
C:phpphp_win c:phptestgtk.php # Means no windows, executes a standalone executable, uses php-q mode, but stops execution whenever any character, such as an error message, is output.

3.2 Installing on UNIX systems
Users of Debian can download php-gtk binary files in System requirements must have the following package installed:

PHP 4.1.0 or later versions must be compiled into a CGI binary (command-line) version, containing all header files and devlement scripts.

PHP-GTK supports GTK + v1.2 and needs to install more than 1.2.6 version of GTK +. GTK + v2.0 is not yet supported and will not be supported until it is developed and popularized. You can get the latest version of GTK + v1.2.x from the URL below:

After you extract the files you have obtained or check out from CVS, switch to the directory and start the installation (call ROM ~):

Get the CVS version, execute
Cvs-d Co PHP-GTK
or download the latest version

2./configure (if you want to add extensions, please do not./configure--help See instructions)
3. Make (if you see "Could not Write┅", just on behalf of the GTK + object has not been supported, is not an error message)
4. Make install

Performing a look at the example scripts in the test/folder to test, especially gtk.php, is a good example of how to use it.

4. The first program

4.1 Preface
This chapter will teach you some commonly used gtkclass (widgets), and use these to make your first PHP-GTK program, if the concept of the chapter is not very familiar, this section can give you a chance to practice Oh! If you do not understand the content of this chapter or want to learn more about other widgets, you can go to see the manual, the manual contains a number of sample programs.

4.2 Widgets to use.
Before you start writing a program, you can use the Widget class to make a overview.

Gtkwindow ()
Gtkwindow () To create a window, there are many ways to use, such as: Set_title,set_name,
Connect,set_border_width and so on.

Gtkframe ()
Gtkframe () simply create a good border, you can set its label name,alignment,
Shadow (in English, reading manual will be more convenient).

Gtkvbox ()
Gtkvbox () Establish an upright container to put into the widgets.

Gtklabel ()
Gtklabel () can create a label, content text can be set up when the setting can be set up after the method to set up, if not set content text, will create an empty label (this is nonsense?).

Gtkhseparator ()
Gtkhseparator () Establish a horizontal line.

Gtkentry ()
Gtkentry () Creates a TextBox for users to enter information.

Gtkhbuttonbox ()
Gtkhbuttonbox () Creates a container that arranges the button horizontally.

Gktbtton ()
Gtkbutton () may be the most commonly used widget in GUI programs, which creates a button that allows the user to press.

4.3 start

If (!class_exist ("GTK"))
DL ("PHP_GTK."). (Strstr (Php_os, "WIN")? "DLL": "So");

This code will determine if the PHP-GTK extension module has been started, and if not, it will read the appropriate file. In the example above, it is judged by whether the operating system being executed is windows or otherwise to be loaded into Php_gtk.dll or

Function delete_event ()
return false;

This creates a function called Delete_event, which is the callback function when Delete-event signal is issued later. The return false tells PHP-GTK to handle with a preset signal handler, and the preset handler closes the window (and calls the Destroy () function of the window), where it closes the program (because the sample program has only one main window, Once closed, the program is closed.

Function Destroy ()
Gtk::main_quit ();

This creates a function, destroy (). In this program, this function is important because we connect to it when we close the program. As I said before, Gtk::main_quit () closes the program, and if we don't define this function in this program or if there is no gtk::main_quit () in this function, then the program won't close. Delete-event mentioned in the preceding code description, the default is to perform the action of closing the window and call destroy () if there is no definition or no gtk::main_quit (). The main window does close, but the program does not end because the main program loops Agtk::main () is still running.

$window = &new Gtkwindow ();
Set a name to identify each window
$window->set_name (' main Window ');
Set the caption of the window
$window->set_title (' Introduction to PHP-GTK ');
Set the size of the window
$window->set_usize (160, 120);
Call Destroy () function to end the program
$window->connect (' Destroy ', ' destroy ');
Call Delete_event () function to close the window
$window->connect (' delete-event ', ' delete_event ');
Set the border width of a window
$window->set_border_width (10);
Set the position of the window
$window->set_position (Gtk_win_pos_center);
Display windows and all child widgets (not visible)
The last two lines must be at the end of the code, otherwise you won't see anything.
$window->show_all ();
Gtk::main ();

The execution program can see the following figure:

Build a Gtkframe
$frame = &new Gtkframe (' Simple modified program ');
Put Gtkframe in the Gtkwindow.
$window->add ($frame);
The bottom two lines don't move.

The results are shown below:

The following section establishes a gtkvbox as a container, and packs Gtkentry, Gtkhseperator, Gtklabel and Gtkbuttonbox into the pack, the so-called pack, It is the container of the Gtkbox to add widgets in a way that is similar to add (), and the method used for pack is generally pack_start () and Pack_end (), than Add () The good place is to be able to control the widget position after adding widgets (but as long as the container will have the Add () method), for more information, please go to

Build a gtkvbox for the common container
$box 1 = &new gtkvbox ();
Put the Gtkvbox in the Gtkframe.
$frame->add ($box 1);
Build a Gtklabel and pack it into gtkvbox.
$label = &new Gtklabel ();
$box 1->pack_start ($label);
Build a gtkhseparator and pack it into gtkvbox.
$separator = &new gtkhseparator ();
$box 1->pack_start ($separator);
Build a gtkentry and pack it into gtkvbox.
$entry = &new gtkentry ();
$box 1->pack_start ($entry);
Build a gtkbuttonbox and add it to the Gtkvbox.
Because Gtkbuttonbox is also an invisible container, position is not important, so use Add ()
$box 2 = &new gtkhbuttonbox ();
$box 1->add ($box 2);

Execute the following figure:

The final code will create two Gtkbutton and pack into the Gtkbuttonbox, and the two button plus connect, make them work, and create a function, as long as the press Gtkbutton will be gtklabel to replace the contents of the text in Gtkentry.

$button = &new Gtkbutton (' Display typed words ');
Connect "clicked" Signal to Set_name () functions, additional $label and $entry two widgets
$button->connect_object (' clicked ', ' Set_name ', $label, $entry);
$box 2->pack_start ($button);
$button = &new Gtkbutton (' Leave program ');
Connecting the "clicked" Signal to the Destroy () function will close the program
$button->connect (' clicked ', ' destroy ');
$box 2->pack_start ($button);

function Set_name ($label, $entry)
Using the Gtkentry Get_text () method to get the text box contents
$gettext = $entry->get_text ();
Use the Gtklabel Set_text () method to set new text
$label->set_text ($gettext);
Finally, mention the two lines.
$window->show_all ();
Gtk::main ();

Writing here, the whole program is finished, let's look at the results of the execute.

5. Other

5.1 Further studies
If you want to learn more about PHP-GTK after you have finished the above course, or have any
Do not understand the place, here to provide you with a few places to query information:

PHP-GTK official website (En):
GTK official website (En):
Manual (En) on the official website of PHP-GTK:
Tim Official website (ZH-TW):

5.2 Another example
Here is a writer to write a guess number game, compared to the example of advanced, you can grasp back to study.

5.3 Reference Data
This article mainly refers to the PHP-GTK official manual and the Zend website tutorial and compiles:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.