Using PHP-GTK2 to do Win32 GUI program _php Tutorial

Source: Internet
Author: User
Tags gtk
PHP is usually done for server-side scripting, and if you tell you that PHP can write normal GUI programs, You should be interested. The PHP-GTK described below is the GUI extension of PHP. GTK is an industry-standard graphics library with good portability. If you've ever used a Linux GNOME desktop environment, it should be no stranger to it, and Gnome is the free software that develops after KDE encounters a QT license barrier, and GTK is its The graphics library used.

PHP-GTK uses component ideas (visible component ideas have been available decades ago), and cannot help but mention TCL/TK languages.

Professor John Ousterhout, TCL/TK's inventor, was a professor at the University of Berkeley in the early 80. In his teaching process, he found that in the design of integrated circuit CAD, a lot of time is spent in programming to establish a test environment. And, once the environment has changed, you need to re-modify the code to accommodate it. This laborious and inefficient approach forced Professor Ousterhout to find a new programming language that would have good code reusability and be easy to learn, thus contributing to the creation of the TCL (Tool Command Language) language. Tcl is often used for rapid prototyping, scripting, GUI and testing.

TCL initially conceived of the desire to program programming in a component-based approach (Component approach), which is to write hundreds of lines of code for a single application, rather than looking for a way to divide the program into small, reusable components with a certain "complete" function. These small components are small enough to basically meet the needs of some independent applications, and others can be built on the basis of these small component functions. Different components have different functions for different purposes. And can be used for other applications. Of course, the language also has a good extensibility to allow users to add new functional modules. Finally, a strong, flexible "glue" is needed to "stick" these components together, allowing each component to "communicate" with each other and work together. Programming is like a jigsaw puzzle, and this design idea coincides with that of the later Java. Finally, in the spring of 1988, this powerful and flexible glue-Tcl language was invented.

In accordance with Professor Ousterhout's definition, TCL is an embeddable command-scripted language (order script Language). "Embeddable" refers to the efficient and seamless integration of many applications.

It's also very similar to the very lively. NET, which is now being scrambled. NET has an IL, intermediate language, you can use the appropriate language to do components, through the IL implementation of module cohesion and reuse. To be blunt, Microsoft's idea is to draw on the tcl/tk of more than 10 years ago, but Microsoft has the money to do it better.

The advanced nature of this structure is self-evident, has good code reusability, easy to learn, has good extensibility, can be reused, execution efficiency is higher than simple script, function is stronger, explanation language, code can change dynamically, platform independent. Win32, UNIX, MAC can run, code compact, easy to maintain debugging simple and so on. This way for many later scripting languages, such as Python,perl, PHP script's nature is also very suitable for this extension, PHP-GTK is only one of its extensions.

PHP-GTK founder Andrei Zmievski recalls the process of PHP-GTK planning and creation:

"I always have a strong interest in GUI programming. I found that GTK + is a very good toolkit. Having witnessed the implementation of PYGTK and Gtk-perl, I decided to try PHP to be able to be made into GTK + interfaces as well. In August 2000, I had more spare time, so I started experimenting. I refer to the implementation of PYGTK because it has a fairly complete implementation and a nice object-oriented interface. James Henstridge,pyg's author, in the initial preparatory stage, provided great help.

"It's almost impossible to write all the interfaces of the GTK + function manually, so I focused on code generation, just like PYGTK did." The code generator is a PHP program that can read a series of. defs files that contain GTK + classes, constants, and method information and generate C code that interfaces with PHP. Parts that cannot be automatically generated can be submitted to a. overrides file.

"The code generator and the infrastructure work took me a certain amount of time, because I spent a few hours in the fall of 2000 on PHP-GTK. After I showed PHP-GTK to Frank Kromann, he became interested and started helping me with the implementation of the code generator and Win32. When we wrote the first Hello World program and tried to make it work, our joy was palpable. Two months later, the project was a success and the build was released on March 1, 2001.

"I've set up a mailing list and a CVS repository for it," PHP-GTK. At the same time, the website is also built up. ”

A simple example, more look at the installation package inside the Demos. PHP-GTK official site for Http://, there is an online manual for inquiry. Currently the highest version of PHP-GTK is PHP-GTK2, and is still being perfected.

A complete run and test package can be downloaded from and run under the command line:

PHP demosxxx.php

You can test the demo program inside.

Believe that the first time to see the presentation of the person will be surprised: the original PHP can really develop WIN32 GUI Desktop program Ah!

The following is an example of saving fame as a mydemo.php file into the Demos folder, running the command:

c:phpgtk2>php demosmydemo.php

Will pop up a small window, you can drag and drop the mouse to change its size, click on the button above, the window is closed, and then in the DOS window output "Hello world!" String.

function Destroy ()
Gtk::main_quit ();

function Hello ()
Global $window;
Print "Hello world!";
$window-Destroy ();
Build a Gtkwindow
$window = &new Gtkwindow ();
Connect the "destroy" signal with the Connect () method to the Shutdown function
$window->connect ("Destroy", "destroy");
Create a Gtkbutton, button text for "Press Me"
$button = &new Gtkbutton ("click Me");
$button, connect ("clicked", "Hello");
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 ();

Mesh Test Testimonials:

PHP can do this step really makes people very feeling! But after feeling, feel strange again.
The first is that PHP is a big line in WEB development, obscuring the light of its desktop development--not yet brilliant. and PHP in web development, especially web site development, although the industry has long been the eldest, but its birthright has been old three, the position of the old four. At this time, then talk about desktop development, then feel is "off-the-table".
The fact is, PHP-GTK this PHP branch is very slow to develop, as of August 2010, its latest update is already 2.5 ago. Either financial distress, human distress, or the prospect of business, the project would have to die at any time, as if a breeze could blow it out.
Desktop development, the popular is VC + +, Delphi, Winform (c#/, and then barely counted on Java, as if there is no PHP anything. I think, since the use of Linux GTK Library, to Linux under the development is not better, why do you have to go to Windows this window to drill? PHP, Python syntax is a bit strange, do not know the habit of VC + + people like it? To say personal preferences, I still like the C #, which absorbs the advantages of C + +, Java and other languages, pure grammar, in line with the habit, can simultaneously develop Webform and Winform programs, should be promising. true techarticle PHP is usually done for server-side scripting, and you should be interested if you tell PHP that you can write common GUI programs. The PHP-GTK described below is the GUI extension of PHP. GTK is an industry landmark ...

  • 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.