1. Introduction to PHP-GTK 1.1 PHP-GTK PHP-GTK is an extension module of PHP, which allows programmers to write independent GUI programs that are executed on the client. This module does not allow GTK+ programs to be displayed in the browser. It was originally developed to write stand-alone GUI programs. 1.2 GTK GTK was originally developed for GIMP, a GUI image processing software. GTK+ is a suite of tools from GIMP. GTK+ evolved from here until it is now central to Gnome (Gnome is a desktop environment). Later, GTK+ has also been promoted to BeOS and Win32, making it the best choice for PHP extension modules. It maintains PHP's cross-platform and can use PHP to develop window interface programs for Linux, BeOS, Windows and other platforms. 2. PHP-GTK Concept 2.1 Preface Next, I will teach you some conceptual things┅Because the concepts in this chapter are very important, so even if you don’t understand it, you still have to understand it slowly, otherwise ┅In the future┅. Also, the following content is not recommended for readers without programming experience, because there are many concepts that can easily be confused. Also, I will use English for the next part that should be used in English, so that everyone will not be overwhelmed when reading foreign documents. Come on! If you don’t understand anything about this chapter, please check PHP-GTK by yourself. Manual: http://gtk.php.net/manual/en/ 2.2 Widget(s) Widget is the 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. Every widget is a class. There are roughly five periods in the life of a Widget: 1. Creation: declaring an object 2. Placement: adding it to a container container) 3. Signal Connection: receive the signal and perform the action (the action it will perform) 4. Display: 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 containers, such as GtkWindow, GtkTable and GtkBox. Other than this, containers are just like other widgets and can be placed in other containers. All containers come from one class─GtkContainer, which itself comes from the GtkWidget class. So container is also a type of widget. 2.4 Signal(s) When the programmer performs an action in the program, the program needs to have an action in response to the user's action. Signals allow programs to know when a user has taken an action and trigger appropriate responses. For example, when the user presses a button (GtkButton) that can open a new window, the program recognizes the request and opens a new window. This can be done via signal. When the button is pressed, the widget will send out a signal, and then the signal will trigger callbacks to generate a new window (GtkWindow). 2.5 Callback(s) Callback is the function that is called up by the signal after the signal is sent. Callback will execute the function and return a value or perform an action. Callback is the handler funciton of signal. It can be the signal's default handler or a programmer-defined function. To create a callback, you must connect the function to the signal. 2.6 Signal Inheritance (Inheritance) Like methods, signals can be inherited by objects. A widget can send any signal that its parent widget can send, as well as its own unique signal. 2.7 Connecting Signals You must specify a callback function for PHP-GTK to respond to the signal when the signal is sent. Connecting a signal to a function can be achieved using the connect() object method. As follows: connect("destroy", "shutdown"); //Create a GtkButton with the button text "Click me" $button = &new GtkButton("Click me"); $button->connect("clicked" , "you_clicked"); //Put the GtkButton into the GtkWindow that is the container $window->add($button); //Show $window and all its child widgets $window->show_all(); //Enter the program Main loop (meaning program startup) gtk::main(); ?> If you execute it, a window will appear with a button saying "Click me". When the button is pressed, the program will execute the you_clicked function Mode. 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; and the "clicked" signal of the $button object is sent when the user presses the button. Will be given away. The last line of gtk::main() must be executed, so as to tell the computer to start executing the program. Since it has started execution, it must have stopped, right? Yes, you can stop it with gtk::main_quit() Programmed. After reading the above examples, some readers may have questions, "What if I want to execute the method of a widget other than the widget that sends the signal?" At this time, another method is used, a connect_object(), which can span Object calls methods or passes other objects as function parameters.The cross-object calling method is as follows: $window->connect_object("destroy", array("gtk","main_quit")) In this way, gtk::main_quit() will be called when the "destroy" signal of the $window object is sent. In this way, the program will eventually be executed. At the end of the introduction to the connection method, let's mention the custom method of adding connect() and connect_object() to the parameters to be passed to the callback function. See example: connect("clicked","who_are_you",$parameter); $button2 = &new GtkButton("Test 2"); //Connect the "clicked" signal to the kill_the_button1 function, append the parameter $button1 $button2->connect_object("clicked","kill_the_button1",$button1); function who_are_you($widget,$parameter){ echo $parameter; } function kill_the_button($button){ $button->destroy(); } ? > Pay attention to those two functions, who_are_you has two parameters, right? What is the first one used for? Why does it appear automatically? Because, the callback function of each signal will add some parameters due to different signals. By default, the parameters of the callback function will be passed in, and basically all signals will pass at least a parameter a to the callback function to generate the object of the signal. So the first parameter of who_are_you is $button1, and the second one is $parameter, which is the new Superman. The kill_the_button function is different~ Because the connect_object() function will call the default parameters of the original signal's callback function, so kill_the_button only has the $button1 parameter appended to the end of connect_object. In this way, kill_the_button can call The method of $button1 or obtain its properties. The destroy method of $button1 is called here, so $button1 will be destroyed. 2.8 Event(s) Event is a type of signal, but its uses and functions are very powerful. As far as signals are concerned, things like signals are built into widgets. Therefore, for example, if GtkWindow does not have a "clicked" signal, it is absolutely impossible for GtkWindow to send a signal such as clicked without using an event signal. . What if event signal is used? Event signal can be added to any widget, so even if the widget does not originally have the function of emitting a "clicked" signal, you can also use add_events() to add a clicked signal to it. What kind of reaction will the event signal do after it? The event signal contains a lot of information. For example, when you use the event signal "key-press-event", it will also record what key you pressed, so the callback function format of the event signal is usually There are two parameters by default. The first one is still the widget that sends the signal, and the second one is $event. This $event is a class, and the properties and methods inside will be different depending on the type of event signal sent. As for the $event class returned by "key-press-event", one of the attributes is keyval, and the content is which key the user pressed. This is often very useful information for a programmer. Therefore, the importance of events cannot be ignored. Even if you don’t understand it at first, you have to slowly integrate into it. This section is also very important. 3. Install PHP-GTK 3.1 Installation under Windows system First, download the windows binary file of HP-GTK from http://gtk.php.net/download.php (version 0.5.1 at the time of writing this article). Next, let’s take a look at the contents of the PHP-GTK 0.5.1 binary file: 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 manual file to start the installation: 1. Copy the contents of php4 to your php installation directory (for example, C:php). 2. Copy the contents of winnt to your winnt folder. On Windows NT or Windows 2000, it is C:winnt, on Window95, 98, and xp, it is C:windows. If there is already php.ini in the folder, there is no need to do this action. 3. Copy the contents of winntsystem32 to your winntsystem32 folder. It is C:winntsystem32 on Windows NT or Windows2000, and C:windowssystem32 on Window95, 98, and xp. 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 started by entering the command (or creating a shortcut) under "Start" - "Execute", such as: C:phpphp -q c:phptestgtk.php ## means not to print out HTTP Header, but keep using this window until you close the program. C:phpphp -q -c php.ini c:gtk.php ## Same as above, but execute the specified php.ini settings. C:phpphp C:phptestgtk.php ## means that the HTTP Header will be printed, but this window will be used until the program is closed. C:phpphp_win C:phptestgtk.php ## means that the window will not be used. After execution, a separate execution program will be executed. It uses php -q mode, but as long as any characters are output, such as an error message, execution will stop. 3.2 Installation under UNIX systems Debian users can download the binary file of PHP-GTK at http://www.debian.org.System requirements must have the following packages installed: PHP 4.1.0 or later, which must be compiled as a CGI binary (command-line) version, including all header files and devlement scripts. PHP-GTK supports GTK+ v1.2 and needs to install GTK+ version 1.2.6 or above. GTK+ v2.0 is not supported yet, you have to wait until it is developed and completed