In respect to our forum post on how to install PHP-GTK on Ubuntu / Linux, we will have a series of howtos on our blog with respect to PHP-GTK – which is a PHP language binding to the GTK+ widget toolkit. With this in mind, you can create pretty robust desktop applications using PHP-GTK.
In this article, we’ll show you how to create menus using PHP-GTK – and because the documentation is somewhat quite bear we thought we’d make an article on various aspects of PHP-GTK you may struggle to understand at first.
How well supported is PHP-GTK?
In comparison to PyGTK (a Python GTK+ language binding) and other language bindings you can use, PHP-GTK is not as widely supported as one would hope so if you are considering using PHP-GTK for most robust application development, you’ll unfortunately need to consider using a different language binding, such as Python.
Many Linux distributions come bundled with all the packages you need to start developing and running PyGTK applications. However, with PHP-GTK, you need to install the dependencies and then compile and install PHP-GTK. If your primary target market is those that will understand how to execute commands via the Terminal and wouldn’t mind doing so, sure, PHP-GTK is a good option – and especially because on Windows, there are pre-compiled binaries available.
All you need to do with the pre-compiled binary is execute your PHP-GTK script against the PHP interpreter executable. On Mac OS X, installation isn’t as easy – it’s essentially as cumbersome as getting PHP-GTK installed on Linux distributions.
How do I install PHP-GTK?
For Linux distributions, a link at the top of this article is to a forum post we have on our Web Hosting Forum with a detailed how to on getting PHP-GTK installed on Linux distributions such as Ubuntu. The primary issue at hand here is that you need to install quite a few packages (which are referred to as “dependencies” – because PHP-GTK depend on these packages prior to you being able to compile and install PHP-GTK).
Once these dependencies are installed, all you need to install after that is “pecl-cairo” (the Cairo Extension) and then compile and install PHP-GTK. However, on Ubuntu, you may also need to do one more step which is relating to libtool.m4. All with easy to follow instructions in the thread itself.
So let’s take a look at the code – the code is commented to explain each part.
public function addMenu()
/* create the menu */
// the “File” and “Help” main menus
$file = new GtkMenuItem(“File”);
$help = new GtkMenuItem(“Help”);
// create the menubar
// we append the File, Help to the MenuBar
$menubar = new GtkMenuBar();
// append the menu options
// this is for the actual menu options
// this is for the “File” menu
$menu = new GtkMenu();
// this is for the “Help” menu
$menu2 = new GtkMenu();
$open = new GtkMenuItem(“Open”);
$save = new GtkMenuItem(“Save As…”);
$quit = new GtkMenuItem(“Quit”);
$about = new GtkMenuItem(“About”);
// connect the signals
// we append the menu items to the MenuBar
// we set the MenuBar as the submenu for the MenuItem “File” and likewise for “Help”
// we the pack the $menubar the MenuBar (which holds File and Help, to the init_vbox)
You can then execute this method where necessary. For example, via the constructor method. Such as:
public function __construct()
$this->addMenu(); // execute the addMenu() method
And that’s it! It’s not as simple as it could be, but once you understand how it works – it’s easy to do again.