How to Use Tk to Make GUI Applications with PHP

Advertisement

Advertisement

PHP/TK implements object oriented bindings for TCL/TK that can be used to create GUI applications. It only works on Unix systems. PHP must be configured to allow dynamic linking with enable_dl = On in the php.ini. The shared object file (tk.so) can be downloaded manually from PECL. The shared object file goes inside your php modules folder which is typically something like /usr/lib/php/modules. Look for the folder with other .so files or refer to your particular distribution documentation. The binary file that comes with the download is a 32-bit ELF so if you want the 64-bit version you have to compile it yourself.

It is not very well supported and can be a big hassle to get up and running on certain distributions. I don't recommend using Tk with PHP, but you can do it. For more information, refer to the official documentation.

#!/usr/local/bin/php -q
<?php
dl('tk.so');
$root = new Tk();
$root->wmTitle('"Hello World"');
$label = new Label($root, '-text "Hello, world!"');
$label->pack('-side left', '-padx 15', '-pady 15');
Tk_MainLoop();

Advertisement

Advertisement