1. PHP-GTK introduction 1.1PHP-GTKPHP-GTK is an extension of PHP, which allows programmers to write programs that are executed on the client and are independent of the GUI. This module does not allow you to view GTK + programs in a browser. it was initially developed to write independent GUI programs. 1.2GTKGTK was originally developed for GIMP, a GUI image processing software. GTK + is G 1. PHP-GTK introduction
1.1 PHP-GTK
PHP-GTK is an extension of PHP that allows programmers to write programs that are executed on the client and are independent of the GUI. This module does not allow you to view GTK + programs in a browser. it was initially developed to write independent GUI programs.
1.2 GTK
GTK was originally developed for GIMP, a GUI image processing software. GTK + is a set tool of GIMP. GTK + has evolved from here until now it has become the center of Gnome (Gnome is a desktop environment ). Later, GTK + has been promoted to BeOS and Win32, making it the best option for PHP extension modules. to maintain PHP across platforms, PHP can be used as Linux, BeOS, windows and other platforms to develop Windows interface programs.
2. PHP-GTK concepts
2.1 Preface
The next step is to teach you a little bit about the concept of this Chapter. because the concept of this chapter is very important, even if you don't understand it, you still need to understand it slowly, or else you will continue to understand it later. In addition, we do not recommend readers who do not have programming experience to read the following content, because there are many ideas that can be easily confused. Also, I will use English for the next part, so that you will not be overwhelmed when reading foreign documents. come on !! If you have any questions about this chapter, refer
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 common widgets are label, button, window, frame, and text box. All widgets come from an abstract basic class-GtkWidget. Every widget is a class
A Widget has five periods in its life:
1. Creation: declaring an object)
2. Placement: add it to a container (adding it to a container)
3. Signal Connection: receives and performs the action (the action it will perform)
4. Display: whether it is visible (whether it is viewable or not)
5. Destruction: closing of a program)
2.3 Container (s)
Container is a widget that can contain other widgets. Most widgets are container, such as GtkWindow, GtkTable, and GtkBox. Besides this, container is similar to other widgets and can be placed in other container. All container comes from a class-GtkContainer, and its own comes from the class of GtkWidget. Therefore, container is also a type of widget.
2.4 Signal (s)
When a program designer performs an action in the program, the program needs an action to respond to the user's action. Signals allows the program to know that the user has performed the action and can trigger a suitable response.
For example, when a user presses a button that can open a new window (GtkButton), the program recognizes this request and opens a new window. This can be done through signal. When the button is pressed, the widget will issue a signal, and then the signal triggers callbacks to generate a new window (GtkWindow ).
2.5 Callback (s)
Callback is the function that is invoked by signal after signal is sent. Callback executes the function to return a value or perform an action. Callback is the handler funciton of signal. It can be the preset handler of the signal or the function defined by the programmer. To create a callback, you must connect the function to signal.
2.6 Signal Inheritance (inherited)
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 the PHP-GTK to respond to signal when signal sends. You can use the connect () object method to connect a signal to a function.
As follows:
// Create a GtkWindow
$ Window = & new GtkWindow ();
// Connect "destroy" signal to the shutdown function using the connect () method
$ Window-> connect ("destroy", "shutdown ");
// Create a GtkButton with the button text "press me"
$ Button = & new GtkButton ("press me ");
$ Button-> connect ("clicked", "you_clicked ");
// Put the GtkButton in the GtkWindow of the container.
$ Window-> add ($ button );
// Display $ window and all its child widgets
$ Window-> show_all ();
// Enter the main loop of the program (that is, the meaning of the program startup)
Gtk: main ();
?>
If you run it, a window is displayed, containing a button with the words "press me", and the you_clicked function is executed when you press the button program. In this program, the "destroy" signal of the $ window object is sent when the user presses "X" in the upper-right corner of the window; the "clicked" signal of the $ button object is sent when the user presses this button. The gtk: main () in the last line must be executed, so that the computer can be told to start executing the program. since the execution has started, must the program be stopped? You can stop the program with gtk: main_quit.
After reading the above examples, some readers may wonder, "What if I want to execute a method for a widget other than a widget that sends signal ?」, At this time, we need to use another method a connect_object (), which can call a method across objects or pass other objects as the parameter of the function. The cross-object call method is as follows:
In this way, when the "destroy" signal of the $ window object is sent, the program will eventually execute the gtk: main_quit () method.
At the end of the connection method, we will discuss how to increase the parameter numbers to be passed to the callback function by customizing connect () and connect_object. See the example below:
$ Parameter = "new Superman ";
$ Button1 = & new GtkButton ("test ");
// Connect "clicked" signal to the who_are_you function, with parameter $ parameter
$ Button1-> connect ("clicked", "who_are_you", $ parameter );
$ Button2 = & new GtkButton ("test 2 ");
// Connect "clicked" signal to the kill_the_button1 function, with parameters $ button1 appended
$ Button2-> connect_object ("clicked", "kill_the_button1", $ button1 );
Note that the two functions, who_are_you, have two parameters, right? First, what is it? Why does it appear automatically ?? Because, the callback function of each signal will be added due to the difference of signal, and some internal parameters of the callback function will be passed in, basically all signal will be passed to the callback function at least a parameter to generate the signal object. So the first parameter of who_are_you is $ button1, and the second parameter is $ parameter, that is, the new Superman. Then the kill_the_button function is different ~ Because the connect_object () function calls the default parameter number of the original signal callback function, kill_the_button only has the value of $ button1 parameter appended to the connect_object function, kill_the_button can call the $ button1 method or obtain its attributes. here, the destroy method of $ button1 is called, and $ button1 is destroyed.
2.8 Event (s)
Event is a type of signal, but it has powerful functions. For signal, signal is built on widgets. for example, if GtkWindow does not have "clicked" signal, gtkWindow is definitely impossible to send signal like clicked. What if event signal is used? Event signal can be added to any widget, so even if the widget does not provide the "clicked" signal function, you can also use add_events () to add the event signal after it is pressed. The event signal contains a lot of information. for example, when you use the "key-press-event" event signal, it also records the buttons you press, generally, there are two parameters in the callback function format of the event signal. The first is the widget that sends the signal, and the second is the $ event. this $ event is a class, the attributes and methods in the message are different because of the type of the event signal. For $ event class returned by "key-press-event", there is a property in it that is keyval, and the content is the key that the user presses. These are often useful information for a programmer. Therefore, the importance of event cannot be ignored. even if you do not understand it at the beginning, you must integrate it slowly. This section is also very important.
3. install PHP-GTK
3.1 Installation in Windows
First, download the Windows binary file of ....hp-gtkfrom http://gtk.php.net/download.php (0.5.1 at the time of writing this article ).
Next let's take a look at the PHP-GTK 0.5.1 binary file content:
Php4 → php and php-gtk binary files
Winnt → default php. ini file
Winntsystem32 → gtk binaries used by extension
Test → several test files
README.txt → Installation Instructions
Start Installation:
1. copy the php4 content to your php installation directory (for example, C: php ).
2. copy the winnt content to your winnt folder. C: winnt on Windows NT or windows and C: Windows on windows 95, 98, and xp. If php. ini already exists in the folder, this action is not required.
3. copy the winntsystem32 content to your winntsystem32 folder. C: winntsystem32 on Windows NT or Windows and C: windowssystem32 on Windows 95, 98, and xp.
4. copy the content of test to the place where you want to execute your script (this step is not necessary ).
How to run the PHP-GTK program:
The PHP-GTK program can be started by entering commands (or creating shortcuts) under START-execute, such as C: phpphp-q c: phptestgtk. php # indicates that the HTTP Header is not printed, but the window is used until the program is closed.
C: phpphp-q-c php. ini c: gtk. php # Same as above, but run the specified php. ini setting.
C: phpphp C: phptestgtk. php # indicates that the HTTP Header is printed,
Use this window until the program is closed
C: phpphp_win C: phptestgtk. php # indicates that the window is not used, and an independent execution program is executed after execution. it uses the php-q mode, but the execution stops as long as the output is used as the keyword, such as the error message.
3.2 Installation on UNIX systems
Debian users can download the binary file of the http://www.debian.org in the PHP-GTK. The following packages must be installed for system requirements:
PHP 4.1.0 or later versions must be compiled into CGI binary (command-line), including all header files and devlement scripts.
PHP-GTK supports GTK + v1.2 and requires installation of GTK + 1.2.6 or later. GTK + v2.0 is not yet supported. it will not be supported until it is developed and popularized. You can get the latest GTK + v1.2.X from the URL below: ftp://ftp.gtk.org/pub/gtk/v1.2/
After decompressing the obtained file or checking out the file from CVS, switch to the directory and start installation ~) :
Obtain the CVS version and 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 install extensions, please enter./configure -- help for instructions)
3. make (if "cocould not write failed" is displayed, it indicates that the GTK + object is not supported yet. it is not an error message)
4. make install
Run the following example scripts in the test/folder to test, especially gtk. php. these are good examples of how to use them.
4. the 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 chapter is not very familiar, this chapter will give you a chance to practice! If you do not understand the content of this chapter or want to learn more about other widgets, you can go to the http://gtk.php.net/manual/en/ to read the manual, there are many examples of the manual.
4.2 widgets
Before you start writing a program, perform an overview on the widgets class that you will use.
GtkWindow ()
GtkWindow () creates a window, which can be used in many ways, such as set_title, set_name,
Connect, set_border_width, and so on.
GtkFrame ()
GtkFrame () is purely a good Border. you can set its label name, alignment,
Shadow (in English, it is easier to read Manual ).
GtkVBox ()
GtkVBox () creates an upright INER to put it into widgets.
GtkLabel ()
You can create a label for GtkLabel (). you can set the content text when creating it, or use the method to set it later. If no content text is set, an empty label will be created (is this nonsense ?).
GtkHSeparator ()
GtkHseparator () creates a horizontal line.
GtkEntry ()
GtkEntry () creates a textbox for users to enter information.
GtkHButtonBox ()
GtkHButtonBox () creates a INER that arranges buttons horizontally.
GktBtton ()
GtkButton () may be the most commonly used widget in GUI programs. it creates a button that allows users to press.
This code determines if the PHP-GTK extension module is started and if not, it reads the appropriate file. In the preceding example, you can determine whether to load php_gtk.dll or php_gtk.so by checking whether the operating system is Windows or other operating systems.
Function delete_event ()
{
Return false;
}
A function named delete_event is created here, which is the callback function when the delete-event signal is issued. When the content is returned false, the PHP-GTK will be told to process it with the preset signal handler, and the preset handler will close the window (also call the destroy () function of the window), here, it closes the program (because the sample program has only one main window, and the program is closed once it is closed ).
Function destroy ()
{
Gtk: main_quit ();
}
Destroy () is created here (). In this program, this function is very important because we will connect to it when closing the program. As mentioned before, Gtk: main_quit () will close the program. if we do not define this function in this program or the function does not contain the line Gtk: main_quit, then the program will not be closed. In the delete-event mentioned in the preceding code description, after return false, the default window close action is executed and the destroy () function is called, if this section is not defined or does not contain the Gtk: main_quit () section, the main window will indeed close, but the program will not end, because the main program loops back aGtk: main () still running.
$ Window = & new GtkWindow ();
// Set the name to identify each window
$ Window-> set_name ('main window ');
// Set the title of the window
$ Window-> set_title ('Introduction to PHP-GTK ');
// Set the window size
$ Window-> set_usize (160,120 );
// Call destroy () to end the program
$ Window-> connect ('deststroy', 'deststroy ');
// Call delete_event () to close the window
$ Window-> connect ('delete-event', 'delete _ event ');
// Set the border width of the 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 displayed)
// The last two lines must be placed at the end of the code; otherwise, nothing can be seen.
$ Window-> show_all ();
Gtk: main ();
?>
The following figure shows the running program:
// Create a GtkFrame
$ Frame = & new GtkFrame ('simple modified Project ');
// Put the GtkFrame in the GtkWindow
$ Window-> add ($ frame );
// Do not move the two rows at the bottom
The result is as follows:
In the following section, create a GtkVBox as the container and pack the GtkEntry, GtkHSeperator, GtkLabel, and GtkButtonBox. The so-called pack is the method that containers under the GtkBox add to the widget, this is similar to add (). the methods used by pack are pack_start () and pack_end (), which are similar to add () the good thing is that you can control the position of the widget after adding the widget (but as long as it is a container, there will be the add () method). For details, go
Http://gtk.php.net/manual/en.
// Create a GtkVBox, which is a commonly used container
$ Box1 = & new GtkVBox ();
// Put the GtkVBox in the GtkFrame
$ Frame-> add ($ box1 );
// Create a GtkLabel and pack it into the GtkVBox
$ Label = & new GtkLabel ();
$ Box1-> pack_start ($ label );
// Create a GtkHSeparator and pack it into the GtkVBox.
$ Separator = & new GtkHSeparator ();
$ Box1-> pack_start ($ separator );
// Create a GtkEntry and pack it into the GtkVBox
$ Entry = & new GtkEntry ();
$ Box1-> pack_start ($ entry );
// Create a GtkButtonBox and add it to the GtkVBox
// Because GtkButtonBox is also an invisible INER and its location is not important, add () is used ()
$ Box2 = & new GtkHButtonBox ();
$ Box1-> add ($ box2 );
Example:
The last code will create two gtkbuttons and pack them into the GtkButtonBox, and add connections to the two buttons to make them work and create a function, press the GtkButton to replace the content of the GtkLabel with the text in the GtkEntry.
$ Button = & new GtkButton ('display input word ');
// Connect "clicked" signal to the set_name () function, and attach two widgets: $ label and $ entry.
$ Button-> connect_object ('clicked', 'set _ name', $ label, $ entry );
$ Box2-> pack_start ($ button );
$ Button = & new GtkButton ('exit Project ');
// Connect "clicked" signal to destroy (). The program will be closed.
$ Button-> connect ('clicked', 'deststroy ');
$ Box2-> pack_start ($ button );
Function set_name ($ label, $ entry)
{
// Use the get_text () method of GtkEntry to obtain the text block content
$ Gettext = $ entry-> get_text ();
// Use the set_text () method of GtkLabel to set the new text
$ Label-> set_text ($ gettext );
}
// Try again later.
$ Window-> show_all ();
Gtk: main ();
When the program is completed, let's take a look at the results of the program ~
5. others
5.1 Further Study
If you want to learn more about PHP-GTK after finishing the course above, or have any
I don't understand. Here are some of the information you can query:
PHP-GTK official website (En): http://gtk.php.net
GTK official website (En): http://www.gtk.org
Manual (En): PHP-GTK on the http://gtk.php.net/manual/en official website
TIM official website (zh-Tw): http://tim.jerry.com.tw
5.2 Another example
Here is a guessing digital game written by the Author. it is a relatively advanced example. you can go back and study it.
Http://pc035860.infor.org/download/GuessNumber.zip
5.3 Reference Data
This article is to refer to the PHP-GTK official Manual and Zend website Tutorial compiled:
Http://gtk.php.net/manual/en
Http://www.zend.com/zend/tut/tutorial-silva.php
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.