![]() |
||||
Menu |
PHP GTK Tutorial #1PHP is known primarily as a server side language for use on the Internet or for Intranet applications. However, it can be used as a standalone interpreted language although the user interface is restricted to text mode terminal windows. However, it is now possible to use the GTK toolkit that provides a number of widgets (buttons, text boxes etc) that make up a modern Graphical User Interface (or GUI and can create programs that look like this:-
This tutorial is built around the Linux operating system although it should be possible to port the source code to others. To get PHP running, please see my previous tutorial To get PHP GTK working, you will need to either download the file or install it using the installer for your particular version of Linux. In Mandrake 10.x, this file is called:- php-gtk-1.0.0-1mdk.i586.rpm and is version 1 of the PHP GTK toolkit. It sjould be noted that PHP GTK does not yet support the later version 2 of the toolkit which has now superceded version 1 and is no longer being updated. The manual notes that some areas of version 1 are prone to errors but I will try to work around them as best I can. You will also need to install GTK although this is usually installed by default by most Linux distros. Finally, you will need to install Glade which is for designing Graphical User Interfaces in a way similar to products like Visual Basic. As this is part of the Gnome project desktop, you will have to install Gnome as well although again this may have been installed already by default. Though a Gnpme program, Glade and all subsequent PHP GTK programs will run in KDE or any other window manager. Well, I think thats enough preamble. If you have difficulty getting PHP GTK working then please send me an email and I'll try to help. To test if all is well, create a new text file (perhaps called gtktest.php or whatever you like) and enter the following code:- <>#! /usr/bin/php -q #! /usr/bin/php -q As this is a standalone program (or script) it
requires this line to tell the command interpreter which
program to use to run this particular script which in
this case is the php program. <?php The PHP interpreter is then told to begin interpreting
instructions and there is a reminder (prefixed by #) to
describe what the script is doing. Here we test for the presence of a particular
Operating System which in this case is Linux and so
defaults to the else statement and loads the relevent PHP
library. if (!extension_loaded('gtk')) We then check for the gtk library. function destroy() There isn't really much for the programmer to do to
display a window as much of the functionality is handled
by the GTK toolkit libraries. However, it is important to
tell GTK that we want to be able to close the program
down so here we use the quit command if the destroy event
is fired which is produced when the user clicks on the
window close icon. $window = &new GtkWindow(); Now to the main part of the script. This command will
create a new window and store it in the variable name
preceded by the $ sign. If you are uncertain about
variables then please refer to my PHP
tutorials. $window->connect('destroy', 'destroy'); Now we connect to the new window and if there is a
destroy event (reading from left to right) then the
function above also called destroy is invoked and the
window is closed down. $window->show_all(); Otherwise, we display the window and all its contents
(although currently it is empty). The GTK main routines are then invoked and the script
ends below. ?> To run this script, open a terminal window and navigate to the folder containg the script and run it using the command:- ./gtktest.php or whatever you chose to call it and a window will spring into life. If you made a mistake then an error will appear in the terminal window stating an error description and a line number. It will then be a matter of changing the file using your favourite editor and trying again. I think that just about wraps it up for the moment. If you are having difficulty with any of this then please send me an email and I'll try to help. Look out for the next tutorial coming soon... |
|||
| Please email: nickjc@nickjc.co.uk for help and advice | ||||
| Back | ||||