Home Backend Development PHP Tutorial 用PHP开发GUI_PHP

用PHP开发GUI_PHP

Jun 01, 2016 pm 12:27 PM
php develop document program compile

环境:W2k php4.3.1 php/gtk0.5.2
一个简单的记事本(只可以打开文件进行修改)
set_time_limit (0); // 设置运行时间

if (!class_exists ("gtk")) // 判断是否有GTK模块
if (strtoupper (substr ($_SERVER["OS"], 0, 3)) == "WIN")
dl ("php_gtk.dll");
else
dl ("php_gtk.so");

$window = &new GtkWindow (); // 建一个窗口
$window->set_uposition (100, 100); // 窗口出现位置
$window->set_usize ((gdk::screen_width()-200), (gdk::screen_height()-150)); // 窗口大小
$window->set_title ("WINDOWS"); // 设置窗口标题
$window->connect_object ('destroy', array ('gtk', 'main_quit')); // 注册窗口的事件

$vbox = &new GtkVBox ();
$hbox = &new GtkHBox ();
$window->add ($vbox);


$menuBar = &new GtkMenuBar (); // 创建菜单
$vbox->pack_start ($menuBar, false, false, 0);

$file = &new GtkMenuItem ("File");
$menuBar->append ($file);

$fileMenu = &new GtkMenu ();
$open = &new GtkMenuItem ("Open");
$save = &new GtkMenuItem ("Save");
$line = &new GtkMenuItem ();
$line->set_sensitive (true);
$exit = &new GtkMenuItem ("Exit");
$fileMenu->append ($open);
$open->connect_object ('activate', 'showFileSelection');
$fileMenu->append ($save);
$save->connect_object ('activate', 'saveFile');
$fileMenu->append ($line);
$fileMenu->append ($exit);
$exit->connect_object ('activate', array ('gtk', 'main_quit'));

$file->set_submenu ($fileMenu);

$scroll = &new GtkScrolledWindow ();
$scroll->set_border_width (8);
$scroll->set_policy (GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
$hbox->pack_start ($scroll, true, true, 0);
$vbox->pack_start ($hbox, true, true, 1);

$text = &new GtkText ();
$text->set_editable (true);
$text->set_word_wrap (true);
$scroll->add ($text);

function showFileSelection () // 文件选择函数
{
$file = &new GtkFileSelection ("File Selection");
$ok_button = $file->ok_button;
$ok_button->connect ('clicked', 'openFile', $file);
$ok_button->connect_object ('clicked', array ($file, 'destroy'));
$cancel_button = $file->cancel_button;
$cancel_button->connect_object ('clicked', array ($file, 'destroy'));
$file->show ();
}

$filePath = null;
function openFile ($button, $f) // 打开文件的函数
{
GLOBAL $text, $save, $filePath;
$filePath = $f->get_filename ();
if (is_file ($filePath))
{
$fp = fopen ($filePath, 'r');
while (!feof ($fp))
$str .= fgets ($fp, 1024);
$text->insert (null, null, null, $str);
fclose ($fp);
return $filePath;
}
}

function saveFile () // 保存文件的函数
{
GLOBAL $filePath, $text;
if (is_file ($filePath))
{
$str = $text->get_chars (0, -1);
$fp = fopen ($filePath, 'w');
fwrite ($fp, $str);
fclose ($fp);
}
return;
}

$window->show_all (); // 显示窗体内的所有控件
gtk::main (); // 最重要的一句,不可少的
?>

附:php/gtk配置

实战PHP/GTK(转载)

刁馋 发表于 2002-2-19 15:25 PHP编程 ←返回版面

以前PHP被认为只能用来编写服务器端的CGI程序,如果说PHP能够开发Windows下的GUI(图形用户界面)程序,你相信吗?最近,PHP的开发小组成功开发出了捆绑GTK 的PHP,那么就可以开发Windows下的GUI程序了。

一、建立PHP/GTK运行环境:
其实GUI程序和普通的PHPCGI程序没有什么区别,不过是PHP/GTK程序由GTK的类来生成GUI界面而已,他们同样是开放源代码,靠PHP来解析建立窗口。如果你已经建立好了PHP的调试环境,那么安装PHP/GTK环境比较简单:
1、下载php_gtk.dll(这个dll文件用于解析PHP源程序里面的GTK代码),下载后将其解压到PHP的扩展(extension)目录中;
2、下载其他PHP/GTK的dll文件(一共6个),将他们解压缩到Windows的system32目录里面;
3、打开php.ini,在扩展设置部分“Windows Extensions”最下面加入“extension=php_gtk.dll”的语句,建议先备份php.ini,避免修改失败使得建立好的PHP运行环境作废;
现在就建立好了PHP/GTK的运行环境了。当然,你也可以不作第二步,而改为在每个PHP/GTK程序源代码的第一行加入“dl('php_gtk.dll')”来动态加载GTK的支持。
如果你还没有建立PHP运行环境,那么安装要更加简单:
1、下载整个PHP/GTK的捆绑支持包,然后将他们解压缩到c盘的PHP4目录下面;
2、将winnt目录下面的dll文件拷贝到Windows的system32目录中,将php.ini文件拷贝到Windows的目录中;
建立好了PHP/GTK的运行后,可以用命令行模式的PHP来运行一个PHP/GTK程序:在运行中输入“c:\php4\php -q gtkprogrampath”,其中“c:\php4\php”改为PHP.EXE的路径,“gtkprogrampath”就是PHP/GTK程序的路径。比如:“c:\php4\php -q c:\php4\samples\hello.php”将会运行PHP/GTK运行包中附带的例子“hello world”程序。

二、编译PHP/GTK程序:
如果你作出一个PHP/GTK的应用程序,还需要对方的电脑建立PHP的运行环境并且使用PHP命令行模式来解析才可以执行的话,那就过于复杂了。那么怎么样才可以编译一个PHP/GTK的程序呢?经过一番搜索,笔者发现了PHPCompiler这个软件。
PHPCompiler由www.deskcode.com开发(http://www.deskcode.com/phpcompiler),可以将PHP编译成为EXE可执行文件,内置了PHP的支持,如果你要编译一个PHP/GTK程序,必须建立PHP/GTK的运行环境(参照前面的步骤)。要编译一个PHP/GTK程序的步骤如下:
1、打开PHPCompiler(如图),在“Script to”选择希望编译的PHP程序源代码,在“Destination”处选择编译成功后的EXE文件的路径;
2、点击“Compile”按钮,会弹出一个对话框询问使用的编译模式(如果是PHP/GTK程序就选择no,是单纯的PHPCGI程序就选择yes);
3、然后又会弹出一个对话框询问是否拷贝php4ts.dll文件到编译后的EXE文件的目录,选择yes。
经过上面的步骤,一个PHP/GTK程序就成功编译了,但是对于编译PHP/GTK程序还有几个需要注意的地方:
1、编译完成后的可执行文件如果需要执行,PC机上面必须具有GTK运行环境所需的几个dll文件(就是前面下载的那几个),如果需要作成应用程序,可以在安装的时候将几个dll文件拷贝到system32目录中(不过这样子程序也就不是“绿色软件”了^_^)。
2、PHPCompiler本身对于PHP的支持非常好,但是有些人试过用一些需要扩展支持的函数,比如“gzopen”等等,在编译之前还好好的,编译完毕后就不能用了。其实编译完成后的可执行文件,相当于了只有默认的PHP支持(连GTK支持都没有了),所以如果在程序中应用了任何需要扩展支持的函数,都必须动态的加载支持函数的dll文件,比如“dl('php_gtk.dll')”,这样子编译完成后才不会出错。
3、运行一个编译后的可执行文件,都会先弹出一个DOS窗口,然后自动关闭,因为每个GUI窗口都是通过GTK来“绘制”出来的,所以必须会有那个窗口。
4、或许以前PHP还不能说是真正的OOP(面向对象程序)语言,但是到了PHP/GTK,任何一个窗口都由GTK对象来“绘制”,如果没有过硬的OOP功底,是很难写出GUI程序的。

三、PHP/GTK资源:
1、http://gtk.php.net:PHP/GTK的官方网站;虽然说是官方网站,但是确简陋的很,出了一个FAQ和邮件列表,几乎找不到任何有用的东西了,
2、http://www.phpgtk.com:一个界面比较好的PHP/GTK网站,有最新的版本信息。
3、http://developer.gnome.org/doc/API/gtk/gtkobjects.html:PHP/GTK函数和类的大全/手册网站,里面有十分丰富的PHP/GTK资料。
4、http://www.phpuk.org/gtk/:非官方版本的GTK手册网站,简单易懂。

如果大家认为E文比较难看,也可以来zphp.com下载最新的PHP/GTK运行环境和支持包。

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles