PHPGangsta - Der praktische PHP Blog

PHP Blog von PHPGangsta


Archive for the ‘PHP Windows Programm’ tag

Windows-Applikation mit PHP-GTK2 erstellen

with 5 comments

Vielleicht habt ihr schon mal davon gehört, aber es wahrscheinlich noch nie genutzt: PHP GTK2.

Diese PHP-Extension ist dafür da, mit PHP eine Client-Oberfläche zu erstellen. Hört sich komisch an, geht aber ziemlich gut und einfach, habe es vor Jahren bereits gehört und heute nun endlich mal ausprobiert.

Hier zuerst ein Screenshot meines „Hello World“ Programms, bevor es zum Code geht:

Den Screenshot habe ich auf meinem Windows-Rechner gemacht, GTK läuft aber auch platformunabhängig unter Linux und Mac.

Als erstes lädt man sich das PHP-GTK2 Paket herunter, ich habe nicht den Installer gewählt sondern das normale zip-Archiv (php-gtk-2.0.1 Windows binary pack). Nach dem Entpacken erhält man einen Ordner „php-gtk2“, in dem unter anderem eine php.exe zu finden ist.

In diesem Ordner wird eine Datei erstellt: example1.php mit folgendem Inhalt:

<?php
if (!class_exists('gtk')) {
    die("Please load the php-gtk2 module in your php.ini\r\n");
}

$wnd = new GtkWindow();
$wnd->set_title('Hello world');
$wnd->connect_simple('destroy', array('gtk', 'main_quit'));

$lblHello = new GtkLabel("Just wanted to say\r\n'Hello world!'");
$wnd->add($lblHello);

$wnd->show_all();
Gtk::main();
?>

Der Start passiert aus der Kommandozeile heraus:

C:\phpgtk\php-gtk2>php.exe example1.php

… und schon öffnet sich das entsprechende Fenster.

Um etwas mehr als das einfache „Hello World“ Progrämmchen zu schreiben fiel mir der Artikel von letztem Dienstag über 7-PDF ein: Warum schreibe ich nicht schnell eine Oberfläche zur PDF-Konvertierung?

Gesagt getan, nach 30 Minuten hatte ich eine ganz rudimentäre Oberfläche inklusive PDF-Konvertierer für 84 Formate:

Einen Stolperstein gab es dabei jedoch: Bei dem GTK2 Paket war keine SOAP-Extension dabei, da mußte ich mir erst noch die passende Datei herunterladen und in die php-cli.ini einbinden. Im PHP-Museum (/php5/php-5.2.5-nts-Win32.zip) habe ich die passende php_soap.dll Datei gefunden.

<?
if (!class_exists('gtk')) {
	die("Please load the php-gtk2 module in your php.ini\r\n");
}

//Create the login window
$wnd = new GtkWindow();
$wnd->set_title('7-PDF Converter');
//Close the main loop when the window is destroyed
$wnd->connect_simple('destroy', array('gtk', 'main_quit'));

//Set up all the widgets we need
$lblCredit   = new GtkLabel('Please provide your data');
//The second parameter says that the underscore should be parsed as underline
$lblUsername = new GtkLabel('_Username', true);
$lblPassword = new GtkLabel('_Password', true);
$lblInFile = new GtkLabel('_File to convert', true);
$lblOutFile = new GtkLabel('P_DF destination filename', true);
$txtUsername = new GtkEntry();
$txtPassword = new GtkEntry();
$txtInFile = new GtkEntry();
$txtOutFile = new GtkEntry();
$btnStart    = new GtkButton('Start');

//Which widget should be activated when the
// mnemonic (Alt+U or Alt+P) is pressed?
$lblUsername->set_mnemonic_widget($txtUsername);
$lblPassword->set_mnemonic_widget($txtPassword);
$lblInFile->set_mnemonic_widget($txtInFile);
$lblOutFile->set_mnemonic_widget($txtOutFile);
//Hide the password
//$txtPassword->set_invisible_char('*');

//Call the login function when the user clicks on Login
$btnStart->connect_simple('clicked', 'convertToPdf', $wnd, $txtUsername, $txtPassword, $txtInFile, $txtOutFile);

//Lay out all the widgets in the table
$tbl = new GtkTable(5, 2);
$tbl->attach($lblCredit, 0, 2, 0, 1);
$tbl->attach($lblUsername, 0, 1, 1, 2);
$tbl->attach($txtUsername, 1, 2, 1, 2);
$tbl->attach($lblPassword, 0, 1, 2, 3);
$tbl->attach($txtPassword, 1, 2, 2, 3);
$tbl->attach($lblInFile, 0, 1, 3, 4);
$tbl->attach($txtInFile, 1, 2, 3, 4);
$tbl->attach($lblOutFile, 0, 1, 4, 5);
$tbl->attach($txtOutFile, 1, 2, 4, 5);

//Add the buttons to a button box
$bbox = new GtkHButtonBox();
$bbox->set_layout(Gtk::BUTTONBOX_EDGE);
$bbox->add($btnStart);

//Add the table and the button box to a vbox
$vbox = new GtkVBox();
$vbox->pack_start($tbl);
$vbox->pack_start($bbox);

//Add the vbox to the window
$wnd->add($vbox);
//Show all widgets
$wnd->show_all();
//Start the main loop
Gtk::main();

function convertToPdf(GtkWindow $wnd, GtkEntry $txtUsername, GtkEntry $txtPassword, GtkEntry $txtInFile, GtkEntry $txtOutFile)
{
	//fetch the values from the widgets into variables
	$strUsername = $txtUsername->get_text();
	$strPassword = $txtPassword->get_text();
	$strinFile = $txtInFile->get_text();
	$strOutFile = $txtOutFile->get_text();

	try {
		$path = realpath(dirname(__FILE__) . '/classes');
		set_include_path($path . PATH_SEPARATOR . get_include_path());

		// create a new object, provide options by array in constructor
		require_once 'SevenPdf/Client.php';
		$sevenPdfClient = new SevenPdf_Client(
			array(
				'username' => $strUsername,
				'password' => $strPassword
			)
		);
		$sevenPdfClient->convertToPdf($strinFile, $strOutFile);
		$dialog = new GtkMessageDialog($wnd, Gtk::DIALOG_MODAL,
				Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, 'convert complete');
		$dialog->set_markup('convert complete');
		$dialog->run();
		$dialog->destroy();
	} catch (Exception $e) {
		$dialog = new GtkMessageDialog($wnd, Gtk::DIALOG_MODAL,
				Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, $e->getMessage());
		$dialog->set_markup($e->getMessage());
		$dialog->run();
		$dialog->destroy();
	}
} ?>

Der Wiederverwendung von PHP-Code und Benutzung für eine Desktop-Anwendung steht also nichts im Weg.

Written by Michael Kliewe

März 22nd, 2010 at 9:25 am