Archive for April, 2008

php gtk application example

Sunday, April 6th, 2008

Have you heard about PHP GTK? You can create GUI php application which can run on every operation system on which php-gtk is installed (Windows, Linux, MacOS).

The programming part is very simple, php-gtk provides you easy intuitive classes to work with.

Here is a short fast example for simple password generator  which I created using php-gtk examples:

 <?php

if (!class_exists('gtk')) {
die("Please load the php-gtk2 module in your php.ini\r\n");
}
 
$wnd = new GtkWindow();
$wnd->set_title('random pass');
$wnd->connect_simple('destroy', array('gtk', 'main_quit'));
 
$txtPassword = new GtkEntry();
 
$tbl = new GtkTable(3, 2);
$tbl->attach($txtPassword, 1, 2, 2, 3);
 
$btnGen = new GtkButton('_Generate');
$btnGen->connect_simple('clicked', 'generate', $txtPassword);
 
$bbox = new GtkHButtonBox();
$bbox->set_layout(Gtk::BUTTONBOX_EDGE);
$bbox->add($btnGen);
 
$vbox = new GtkVBox();
$vbox->pack_start($tbl);
$vbox->pack_start($bbox);
 
$wnd->add($vbox);
$wnd->show_all();
Gtk::main();
 
function generate(GtkEntry $txtPassword)
{
$chars = "abcdefghijkmnopqrstuvwxyz023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7) {
$num = rand() % 56;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
$random=$pass;
$txtPassword->set_text($random);
}
?>

 

 Running this: D:\php-gtk\php-gtk2>php myproj/random_pass_gtk.php