PHP-GTK Introduction and its application _php

Source: Internet
Author: User
Keywords application and Introduction one program signal CAN W
Tags gtk signal handler
1. PHP-GTK Introduction

1.1 PHP-GTK
PHP-GTK is a php extension module that allows programmers to write programs that are executed on the client side and are independent of the GUI. This module does not allow GTK + programs to be viewed on the browser, it was developed to write a standalone GUI program at the outset.

1.2 GTK
GTK was originally developed for GIMP, a GUI for image processing software. GTK + is a suite tool for GIMP. GTK + has evolved from here until now it has become the center of GNOME (Gnome is a desktop environment). GTK + has also been extended to BeOS and Win32, making it the best choice for PHP extension modules, maintaining PHP's ability to cross-platform and use PHP to develop Windows interface programs for platforms such as Linux,beos,windows.

2. PHP-GTK Concept

2.1 Preface
The next step is to teach you a little more conceptual stuff, Luo, because the concept of this chapter is very important, so even if you do not understand, it is necessary to slowly understand it, or else after that. Also, the next content is not recommended for readers without programming experience, because there are many ideas that can easily be confused. Also, the next part of the English language I will use in English, so that everyone in the reading of foreign documents will not be overwhelmed, refueling it!! If you do not know anything about this chapter, please check it yourself.
PHP-GTK manual:http://gtk.php.net/manual/en/

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

A widget has about five periods of life:
1. Establishment (Creation): Declaring an object (declaring an object)
2. Placement (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. Display: Whether it is visible (whether it is viewable or not)
5. Delete (destruction): Close Program (closing of a programs)

2.3 Container (s)
Container is a widget that can contain other widgets. Most of the widgets are container, for example: Gtkwindow, gtktable, and Gtkbox. In addition to this, container is no different than the other widgets and can be placed in other container. And all the container come from a class─gtkcontainer, which itself comes from the Gtkwidget class. So container is also one of the widgets.

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

For example, when the user presses a button (Gtkbutton) that opens a new window, the program recognizes the request and opens a new window. The matter can be done by signal. When the button is pressed, the widget emits a signal, and 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 of the signal or a function defined by the programmer. To create a callback, you must connect the function to signal.

2.6 Signal Inheritance (inheritance)
Like 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 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 the $window object is sent when the user presses the "X" in the upper-right corner of the window, while the "clicked" signal of the $button object is sent when the user presses the button. The last line of the Gtk::main () is necessary to execute, so as to tell the computer to start executing the program, since there is a beginning to execute, then there must be a stop it? Yes, you can stop the program with Gtk::main_quit ().

After reading the above example, some readers may wonder, "What if I want to do a widget that's outside of the signal widget?", at this point, we're going to use another method, a connect_object (), It can call methods across objects or pass other objects as three numbers of function. The cross-object call method is as follows:

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


In this way, when the "destroy" signal of the $window object is sent out, the Gtk::main_quit () method is called, and the program eventually executes.

At the end of the introduction to the connection method, mention the way that the custom increase of connect () and Connect_object () is passed to the callback function. See Example:
$parameter = "New Superman";
$button 1 = &new Gtkbutton ("test");
Connect the "clicked" signal to the Who_are_you function with three additional numbers $parameter
$button 1->connect ("clicked", "Who_are_you", $parameter);
$button 2 = &new Gtkbutton ("Test Two");
Connect the "clicked" signal to the Kill_the_button1 function with three additional numbers $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 have two three, right? What's the first thing to do? Why does it automatically appear?? Because, each signal callback function will be due to signal different and some of the default will be passed to the callback function of the three, and basically all signal will be passed at least callback Function a three-number-a object that produces the signal. So Who_are_you's first three numbers are $button1, and the second one is $parameter, the new Superman. That Kill_the_button function is not the same Luo ~ because Connect_object () function will call the original signal callback function preset three numbers, so Kill_the_button is only attached to connect_ The last $button1 of object, so that Kill_the_button can call $button1 method or get its properties, here called the Destroy method of $button1, $button1 will be destroyed.

2.8 Event (s)
The event is one of the signal, but its purpose and function are very powerful. As far as signal, signal this kind of thing is built on the widget, so, for example Gtkwindow does not have "clicked" signal, then in the case of no event signal, Gtkwindow is the impossible to send out clicked such signal. What if we use the event signal? The event signal is allowed to be added to any widget, so you can use Add_events () to add it to the event if the widget does not have a "clicked" signal function. What kind of reaction would signal do? The event signal contains a lot of information, such as when you use the "key-press-event" event signal, but also record what button you pressed, the usual event signal callback The function format will have two three numbers, the first one is still sent out signal widget, and the second is $event, the $event is a class, the properties and methods will be sent over the event signal the kind of different. In terms of the $event class returned by "Key-press-event", there is a property in Keyval which is the content of which key the user presses. These are often useful information for a programmer. So the importance of the event can not be ignored, even if the beginning will be a little confused, but also slowly into the line. This section is also very important.

3. Installing PHP-GTK

3.1 Installing under Windows system
First to download from http://gtk.php.net/download.php ... HP-GTK's Windows Binary Archive (version 0.5.1 When this article is written).

Then take a look at the contents of php-gtk 0.5.1 binary file:
php4→php and PHP-GTK binary files
winnt→ Pre-set php.ini file
WINNTSYSTEM32→GTK binaries used by extension
test→ a few test files.
readme.txt→ Installation instruction File

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

How to execute the PHP-GTK program:
The PHP-GTK program can be launched under "Start"-"Execution" (or create a shortcut), such as: C:phpphp-q c:phptestgtk.php # # means not sending out the HTTP Header, but always use this window until the program is closed.
C:phpphp-q-C php.ini c:gtk.php # # IBID., but performs the specified php.ini settings.
c:phpphp c:phptestgtk.php # # indicates that the HTTP Header will be printed, but has been made
Use this window until the program is closed
C:phpphp_win c:phptestgtk.php # # means that you do not use Windows, the execution of a separate execution program, he is using PHP-Q mode, but as long as the output of any character, such as an error message, will stop execution.

3.2 Installing under UNIX systems
Debian users can download php-gtk binary files from the http://www.debian.org. System requirements must have the following package installed:

The version of PHP 4.1.0 or later must be a CGI binary (command-line) version, containing all headers files and devlement scripts.

PHP-GTK supports GTK + v1.2 and requires the installation of 1.2.6 + versions of GTK +. GTK + v2.0 has not been supported yet and will not be supported until it has been developed and popularized. You can get the latest version of GTK + v1.2.x from the following URL: ftp://ftp.gtk.org/pub/gtk/v1.2/

After the extracted files have been decompressed or checked out by CVS, switch to the directory and start the installation (command ROM ~):

Get CVS version, execute
Cvs-d Server:cvsread@cvs.php.net:/repository Co PHP-GTK
or download the latest version
http://gtk.php.net/download.php

1../buildconf
2./configure (if you want to add extensions, please lose./configure--help See instructions)
3. Make (If you see "Could not Write┅", just to represent that GTK + object is not supported, it is not a false message)
4. Make install

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

4. First Program

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

4.2 will use the widgets
Before you start writing a program, make a overview of the widget classes that you use to peer.

Gtkwindow ()
Gtkwindow () creates a window with many ways to use it, such as: Set_title,set_name,
Connect,set_border_width and so on.

Gtkframe ()
Gtkframe () Pure build a good border, you can set its label name,alignment,
Shadow (in English, it will be more convenient to read manual).

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

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

Gtkhseparator ()
Gtkhseparator () establishes a horizontal line.

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

Gtkhbuttonbox ()
Gtkhbuttonbox () establishes a container that arranges buttons horizontally.

Gktbtton ()
Gtkbutton () is probably the most commonly used widget in GUI programs, and it creates a button that lets the user 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 is started, and if not, it will read the appropriate file. In the example above, it is up to the judge whether the operating system is windows or something else to determine whether to load Php_gtk.dll or php_gtk.so.


Function delete_event ()
{
return false;
}


This establishes a function called Delete_event, which is the callback function of the delete-event signal when it is issued. False tells PHP-GTK to process with a preset signal handler, and the preset handler closes the window (which also calls the Destroy () function of the window), where it closes the program (because the sample program has only one main window, The program is closed once it is closed).

Function Destroy ()
{
Gtk::main_quit ();
}


This establishes a function, destroy (). In this program, this function is important because we are connected to it when we close the program. Previously said, Gtk::main_quit () will close the program, if we do not define this function in the program or the function is not gtk::main_quit () This line, then the program will not be closed. The delete-event mentioned in the code description above, return false after the default will be executed to close the window action, will also call the Destroy () function, if there is no definition or no gtk::main_quit () This paragraph, The main window does close, but the program does not end, because the main program loop Agtk::main () is still running.

$window = &new Gtkwindow ();
Set a name to identify each window
$window->set_name (' main Window ');
Set the title of the window
$window->set_title (' Introduction to PHP-GTK ');
Setting 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 the window
$window->set_border_width (10);
Setting the location of the window
$window->set_position (Gtk_win_pos_center);
Display windows and all child widgets (not visible)
The last two lines must be placed at the end of the code, or nothing will be seen.
$window->show_all ();
Gtk::main ();
?>


The execution program can see the following diagram:





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


Results such as:





The following paragraph establishes a gtkvbox as a container, and puts Gtkentry, Gtkhseperator, Gtklabel and Gtkbuttonbox in packs, so-called packs, Is the Gtkbox under the container to add the method of the widget, just like add (), and the method of pack is generally pack_start () and Pack_end (), than the Add () The good place is to control the widget's location after adding the widget (but as long as the container will have the Add () method), for more information please go to
http://gtk.php.net/manual/en.


Build a gtkvbox, for the common container
$box 1 = &new gtkvbox ();
Put the gtkvbox inside the gtkframe.
$frame->add ($box 1);
Build a Gtklabel and pack it into the Gtkvbox.
$label = &new Gtklabel ();
$box 1->pack_start ($label);
Build a gtkhseparator and pack it into the Gtkvbox.
$separator = &new gtkhseparator ();
$box 1->pack_start ($separator);
Build a gtkentry and pack it into the 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);



Perform such as:





The last code will build two Gtkbutton and pack into the Gtkbuttonbox, as well as connect the two buttons, make them work, and create a function, as long as you press Gtkbutton to change the contents of the Gtklabel to gtkentry text.

$button = &new Gtkbutton (' Show input word ');
Connect "clicked" Signal to Set_name () function, add $label and $entry two widgets
$button->connect_object (' clicked ', ' Set_name ', $label, $entry);
$box 2->pack_start ($button);
$button = &new Gtkbutton (' Leave the 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)
{
Use Gtkentry's Get_text () method to get the text box content
$gettext = $entry->get_text ();
Use Gtklabel's Set_text () method to set new text
$label->set_text ($gettext);
}
Finally, let's mention those two lines.
$window->show_all ();
Gtk::main ();


Write here, the whole program is finished, and look at the results.






5. Other

5.1 Further studies
If you want to know more about PHP-GTK after you finish the course above, or if you have any information about the contents of this article
Don't understand where, here are a few places you can find information:

PHP-GTK official website (En): http://gtk.php.net
GTK official website (En): http://www.gtk.org
PHP-GTK on the official website of Manual (En): http://gtk.php.net/manual/en
Tim Official website (ZH-TW): http://tim.jerry.com.tw

5.2 Another example
Here is the author of the Guess Number game, is a comparison of advanced examples, you can grab back to study to see.
Http://pc035860.infor.org/download/GuessNumber.zip

5.3 Reference Data
This article is mainly compiled by referring to the tutorial on the official manual and Zend website of PHP-GTK:
http://gtk.php.net/manual/en
http://www.zend.com/zend/tut/tutorial-silva.php
  • 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: info-contact@alibabacloud.com 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.