Develop a GUI using PHP

Source: Internet
Author: User
Environment: W2k + php4.3.1 + phpgtk0.5.2 a simple Notepad (only files can be opened for modification) & lt ;? Phpset_time_limit (0); & nbsp; set the running time if (! Class_exists (gtk) & nbsp; determine whether there is a GTK module if (strtoupper (substr ($ _ SERVE Environment: W2k + php4.3.1 + php/gtk0.5.2
A simple Notepad (only files can be opened for modification)
Set_time_limit (0); // Set the running time

If (! Class_exists ("gtk") // determines whether a GTK module exists.
If (strtoupper (substr ($ _ SERVER ["OS"], 0, 3) = "WIN ")
Dl ("php_gtk.dll ");
Else
Dl ("php_gtk.so ");

$ Window = & new GtkWindow (); // create a window
$ Window-> set_uposition (100,100); // the position where the window appears.
$ Window-> set_usize (gdk: screen_width ()-200), (gdk: screen_height ()-150); // window size
$ Window-> set_title ("WINDOWS"); // you can specify the window title.
$ Window-> connect_object ('deststroy', array ('gtk ', 'Main _ quit'); // registers window events

$ Vbox = & new GtkVBox ();
$ Hbox = & new GtkHBox ();
$ Window-> add ($ vbox );


$ MenuBar = & new GtkMenuBar (); // create a menu
$ 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 selection function
{
$ File = & new GtkFileSelection ("File Selection ");
$ OK _button = $ file-> OK _button;
$ OK _button-> connect ('clicked', 'openfile', $ file );
$ OK _button-> connect_object ('clicked', array ($ file, 'deststroy '));
$ Cancel_button = $ file-> cancel_button;
$ Cancel_button-> connect_object ('clicked', array ($ file, 'deststroy '));
$ File-> show ();
}

$ FilePath = null;
Function openFile ($ button, $ f) // function for opening a file
{
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, $ str );
Fclose ($ fp );
Return $ filePath;
}
}

Function saveFile () // function for saving files
{
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 (); // display all controls in the window
Gtk: main (); // The most important sentence.
?>

Appendix: php/gtk configuration

PHP/GTK (reprinted)

Published on at PM in PHP Programming Forum

In the past, PHP was considered to be only used to write CGI programs on the server. If PHP can develop GUI (Graphical User Interface) programs in Windows, do you believe this? Recently, the PHP development team has successfully developed a PHP program bound with GTK +, so you can develop GUI programs in Windows.

1. create a PHP/GTK runtime environment:
In fact, the GUI program is no different from the common PHPCGI program, but the PHP/GTK program is generated by the GTK class to generate the GUI. They are also open source code and rely on PHP to parse and create a window. If you have already set up the PHP debugging environment, it is relatively simple to install the PHP/GTK environment:
1. download php_gtk.dll (this dll file is used to parse the GTK code in the PHP source program) and decompress it to the extension directory of PHP;
2. download other PHP/GTK dll files (6 in total) and decompress them to the system32 directory of Windows;
3. open php. ini: add the "extension = php_gtk.dll" statement at the bottom of "Windows Extensions" in the extended settings section. we recommend that you back up php first. ini to avoid modification failure and invalidate the established PHP runtime environment;
Now the PHP/GTK runtime environment has been established. Of course, you can add "dl ('php _ GTK. dll ') to dynamically load GTK support.
If you have not created a PHP runtime environment, it is easier to install it:
1. download the entire PHP/GTK bundle support package and decompress it to the PHP4 directory of drive C;
2. copy the dll file under the winnt directory to the system32 directory of Windows, and copy the php. ini file to the directory of Windows;
After PHP/GTK is set up, you can run a PHP/GTK program in command line mode: Enter "c: \ php4 \ PHP-q gtkPRogrampath" in the running state ", change "c: \ php4 \ php" to PHP. EXE path. "gtkprogrampath" is the path of the PHP/GTK program. For example, "c: \ php4 \ php-q c: \ php4 \ samples \ hello. php" will run the example "hello world" program included in the PHP/GTK running package.

2. Compile the PHP/GTK program:
If you create a PHP/GTK application, and you need to build a PHP running environment on the other computer and use the PHP command line mode for parsing to execute the application, it is too complicated. So how can we compile a PHP/GTK program? After some searches, I found the PHPCompiler software.
PHPCompiler is developed by www.javascode.com (http://www.deskcode.com/phpcompiler), which can compile PHP into an EXE executable file with built-in PHP support. if you want to compile a PHP/GTK program, you must create a runtime environment for PHP/GTK (refer to the previous steps ). To compile a PHP/GTK program, follow these steps:
1. open PHPCompiler (), select the source code of the PHP program to be compiled in "Script to", and select the path of the EXE file after compilation in "Destination;
2. click the "Compile" button, and a dialog box will pop up asking about the compilation mode. (if it is a PHP/GTK program, select no. if it is a pure PHPCGI program, select yes );
3. then, a dialog box is displayed asking whether to copy the php4ts. dll file to the Directory of the compiled EXE file. select yes.
After the above steps, a PHP/GTK program is successfully compiled, but there are several notes for compiling the PHP/GTK program:
1. if you need to execute the compiled executable files, the PC must have several dll files required for the GTK Runtime Environment (the ones downloaded earlier). If you need to build an application, several dll files can be copied to the system32 directory during installation (however, the subroutine is not "green software" ^_^ ).
2. PHPCompiler supports PHP very well, but some people have tried to use some functions that need to be extended, such as "gzopen". it should be well prepared before compilation, it cannot be used after compilation. In fact, after compilation, the executable file is equivalent to only the default PHP support (even GTK support is not available). Therefore, if you have applied any function that requires extension support in the program, all dll files supporting functions must be dynamically loaded, such as "dl ('php _ gtk. dll.
3. when running a compiled executable file, a DOS window is displayed and closed automatically, because each GUI window is "drawn" by GTK, so there must be that window.
4. in the past, PHP may not be the real OOP language. However, when PHP/GTK is reached, any window is "drawn" by the GTK object ", without excellent OOP skills, it is difficult to write GUI programs.

III. PHP/GTK resources:
1. http://gtk.php.net: PHP/GTK official website; although said it is the official website, but it is very simple, out of a FAQ and mail list, almost can not find any useful thing,
2, http://www.phpgtk.com: a better interface PHP/GTK website, with the latest version information.
3. renewal.
4. http://www.phpuk.org/gtk/:non-official gtkmanual website, which is easy to understand.

If you think E is ugly, you can also download the latest PHP/GTK runtime environment and support package from zphp.com.

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.