Home  >  Article  >  Backend Development  >  Use PHP-GTK2 to create Win32 GUI programs_PHP tutorial

Use PHP-GTK2 to create Win32 GUI programs_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:39:55827browse

    PHP通常是做为服务器端脚本执行,如果告诉你PHP可以编写普通的GUI程序,你应该很感兴趣.下面介绍的PHP-GTK就是PHP的GUI扩展.GTK是一个业界标准的图形库,具有良好的移植性.如果你用过linux的gnome桌面环境,对它应该不会陌生,Gnome就是在KDE遇到Qt许可证障碍后发展的自由软件,GTK则是其采用的图形库。

    PHP-GTK采用的是组件思想(可见组件思想几十年前就有了),为此不能不提到tcl/tk语言。

    Tcl/Tk 的发明人 John Ousterhout 教授在八十年代初,是伯克利大学的教授。在其教学过程中,他发现在集成电路 CAD 设计中,很多时间是花在编程建立测试环境上。并且,环境一旦发生了变化,就要重新修改代码以适应。这种费力而又低效的方法,迫使 Ousterhout 教授力图寻找一种新的编程语言,它即要有好的代码可重用性,又要简单易学,这样就促成了Tcl (Tool Command Language) 语言的产生。TCL经常被用于 快速原型开发,脚本编程,GUI和测试等方面。

    Tcl 最初的构想的是希望把编程按照基于组件的方法 (Component Approach),即与其为单个的应用程序编写成百上千行的程序代码,不如寻找一个种方法将程序分割成一个个小的, 具备一定“完整”功能的,可重复使用的组件。这些小的组件小到可以基本满足一些独立的应用程序的需求,其它部分可由这些小的组件功能基础上生成。不同的组件有不同的功能,用于不同的目的。并可为其它的应用程序所利用。当然, 这种语言还要有良好的扩展性, 以便用户为其增添新的功能模块。最后,需要用一种强的,灵活的“胶水”把这些组件“粘”合在一起, 使各个组件之间可互相“通信”,协同工作。程序设计有如拼图游戏一样,这种设计思想与后来的 Java 不谋而合。终于在1988 年的春天, 这种强大灵活的胶水 - Tcl 语言被发明出来了。

    按照 Ousterhout 教授的定义,Tcl 是一种可嵌入的命令脚本化语言 (Command Script Language)。“可嵌入”是指把很多应用有效,无缝地集成在一起。

    说起来也很类似于现在炒做的很热闹的.NET,.NET有一个IL,中间语言,你可以用合适的语言做组件,通过IL实现模块的衔接和重用。说白了,微软这个思想就是借鉴了十几年前的tcl/tk,只不过微软有钱可以做得更好而已。

    这种结构的先进性不言而喻,有好的代码可重用性,简单易学,有良好的扩展性,可重用,执行效率比单纯的脚本高,功能也更强,解释语言,代码能够动态的改变,平台无关。Win32, UNIX, Mac 上都可以跑,代码紧凑,易于维护调试简单等等。这种方式为后来很多脚本语言采用,如python,perl 等,php脚本的天性也非常适合做这种扩展,php-gtk只是其扩展的一种。

    PHP-GTK创始人Andrei Zmievski 回忆了 PHP-GTK 计划和创建的过程:

    “我总是对 GUI 编程有着浓厚的兴趣。我发现 Gtk+ 是一个非常好的工具包。目睹了 PyGtk 和 GTK-Perl 的实现,我决定尝试 PHP 是否也能够被做成 Gtk+ 的接口。2000年8月,我有了更多的业余时间,于是我开始试验。我参考了 PyGtk 的实现方法,因为它有着相当完全的实现和漂亮的面向对象的接口。James Henstridge,PyG 的作者,在最初的筹备阶段提供了极大的帮助。

    “手工写 Gtk+ 函数全部的接口几乎是不可能的事情,所以我把重点放在了代码生成上,就像 PyGtk 做的一样。代码生成器是一个PHP 程序,它可以读取包含 Gtk+ 类、常量和方法信息的一系列的 .defs 文件并生成与 PHP 相接口的 C 代码。无法自动生成的部分可以提交到 .overrides 文件。

    “代码生成器和基础结构的工作花了我一定的时间,因为我在 2000 年秋天这段时间里只能花很少的时间在 PHP-GTK 上。在我把 PHP-GTK 给 Frank Kromann 演示过后,他产生了浓厚的兴趣,并开始帮助我完成代码生成器和 Win32 的实现。当我们写了第一个 Hello World 程序并设法使之工作之时,我们的喜悦之情溢于言表。两个多月后,这个工程初见成效,并于 2001 年 3 月 1 日发布了内部版本。

    “感到 PHP-GTK 前景广阔,我便为它专门设置了邮件列表和 CVS 知识库。同时,gtk.php.net 网站也建立起来。”

A simple example, see more Demos in the installation package. The official website of php-gtk is http://gtk.php.net, and there is an online manual for query. Currently, the highest version of php-gtk is PHP-GTK2, which is still being further improved.

You can download a complete running and testing software package from http://gtk.php.net/download.php and run it from the command line:

php demosxxx.php

You can test the demo program included.

I believe people who see the demonstration for the first time will be surprised: it turns out that PHP can really develop Win32 GUI desktop programs!

The following is an example, save the file named mydemo.php to the demos folder and run the command:

C:phpgtk2>php demosmydemo.php

A small window will pop up. You can drag it with the mouse to change its size. Click the button above, the window will close, and then the "Hello World!" string will be output in the DOS window.

function destroy()
{
Gtk::main_quit();
}

function hello()
{
global $window;
print "Hello World! ", ) method is connected to the shutdown function
$window->connect("destroy", "destroy");
//Create a GtkButton with the button text "Click me"
$button = &new GtkButton ( "Click Me ");
$button-> connect("clicked", "hello");
//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 main loop of the program (which means program startup)
gtk::main();
?>

Mesh test comments:

It’s really impressive that PHP can do this! But after sighing, I felt weird again.
First of all, PHP's popularity in web development has obscured its brightness in desktop development--it's not even glorious. In terms of Web development, especially website development, although PHP has long been the leader in the industry, its status has always been the third or fourth position. At this time, when we talk about desktop development, we feel that it is "not doing our job properly".

The fact is that the PHP branch of PHP-GTK is developing very slowly. As of August 2010, its last update was 2 and a half years ago. Either there are financial problems, there are manpower problems, or there is no business prospect. In short, this project will be stillborn at any time, as if a breeze can blow it out.

Desktop development, the popular ones are VC++, Delphi, Winform (C#/VB.NET), and barely counting Java, it seems that PHP has nothing to do with it. I think that since we are borrowing the GTK library under Linux, wouldn't it be better to develop under Linux? Why do we have to drill through the window of Windows? The syntax of PHP and Python is a bit weird. I wonder if people who are used to VC++ will like it? Speaking of personal preferences, I still like C# the best. It absorbs the advantages of C/C++, JAVA and other languages. Its syntax is pure and consistent with habits. It can develop Webform and Winform programs at the same time. It should have a bright future.





http://www.bkjia.com/PHPjc/486265.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/486265.htmlTechArticlePHP is usually executed as a server-side script. If I tell you that PHP can write ordinary GUI programs, you should be very happy. Interested. The PHP-GTK introduced below is the GUI extension of PHP. GTK is an industry standard...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn