Introduction and Application of PHP-GTK

Source: Internet
Author: User

1. PHP-GTK Introduction 1.1 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 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 next we will teach you a little bit more conceptual things Luo Yi because the concept of this chapter is very important, so even don't understand, also need to slowly understand it, otherwise, it will be restored after renewal. 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 incomprehension of this chapter, read the PHP-GTK Manual: http://gtk.php.net/manual/en/ 2.2 Widget (s) Widget as a 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 stages in its life: 1. creation: declaring an object 2. placement: add it to a container. signal Connection: receives signals and performs actions (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 programmer performs an action in a 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 called 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 (Inheritance) is the same as methods, and 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 is sent. You can use the connect () object method to connect a signal to a function. As follows: Connect ("destroy", "shutdown"); // create a GtkButton. The button text is "press me" $ button = & new GtkButton ("press me "); $ button-> connect ("clicked", "you_clicked"); // place 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, 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: $ window-> connect_object ("destroy", array ("gtk", "main_quit, when the "destroy" signal of the $ window object is sent, the program will invoke the gtk: main_quit () method, and the program will eventually execute it. 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: Connect ("clicked", "who_are_you", $ parameter); $ button2 = & new GtkButton ("Test 2"); // connect "clicked" signal to the kill_the_button1 function, additional 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 () ;}?> 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 in Windows first download the Windows binary file from http://gtk.php.net/download.php...hp-gtk( 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 file winnt → default php. ini file winntsystem32 → gtk binaries used by extension test → several test files README.txt → installation instructions: 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 instructions (or creating shortcuts) under START-run, 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 will be printed, but this window will be used until the program C: phpphp_win C: phptestgtk is closed. 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 Debian users installed on UNIX systems can download the binary file of the http://www.debian.org from the PHP-GTK. The following package must have been installed: PHP 4.1.0 or a later version. It 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 supported yet. You must wait until it is developed and

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.