PHPGangsta - Der praktische PHP Blog

PHP Blog von PHPGangsta


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

5 Responses to 'Windows-Applikation mit PHP-GTK2 erstellen'

Subscribe to comments with RSS or TrackBack to 'Windows-Applikation mit PHP-GTK2 erstellen'.

  1. Nett, man sollte noch erwähnen, dass man auch komplexe Oberflächen mit Glade nach dem RAD-Prinzip entwickeln kann: http://glade.gnome.org
    *hm* Ich sollte mich evtl. auch mal damit verstärkt beschäftigen…PHP sowohl im Browser für die eine Aufgabe, als auch auf dem Desktop für die andere Aufgabe wäre bei manch einem Projekt sehr sinnvoll.
    Danke für den tollen Artikel; mir fehlt nur die Info, wenn ich das ganze nicht im gleichen VZ haben möchte wie die php-gtk Sachen, wie ich das ganze dann aufrufe.

    Sascha Presnac

    22 Mrz 10 at 10:08

  2. Wow, super Beitrag! Ich wollte mich auch schon ewig damit beschäftigen. Also da werde ich bald mal damit Testen. Mit GTK habe ich unter C++ schon gearbeitet und fand es nicht schlecht. Das es aber so einfach geht, hätte ich jetzt nicht gedacht!

    Grüße
    Tobi

    Tobi

    22 Mrz 10 at 19:44

  3. Ich habe mich auch daran versucht und war begeistert, leider kann man auf der oben beschrieben Installation kein MySQL o.ä. nutzen. Der Versuch die php_mysql.dll einzubinden funktionierte leider auch nicht (zum. bei mir).
    Vielleicht kannst du dazu einen Artikel veröffentlichen, da ich ungern auf Java & Swing zurückgreifen möchte.

    Gruß
    Marc

    Marc

    5 Aug 10 at 18:31

  4. Falls jemand was mit Grafiken — also GD Erweiterung — machen will:
    Ich hab auf meinem Blog auch mal ein kleines zip mit PHP-GTK 2.0.1 ink. GD geschnürt.
    War recht schwer zum laufen zu kriegen, evtl. spar ich jemandem den Aufwand.
    URL ist http://www.geek-blog.de/php-gtk-2-0-1-inklusive-gd-fur-windows.html

    Have Fun
    Flo

    Florian Löffler

    10 Nov 10 at 23:30

  5. Hallo,

    guter Artikel! Ich möchte hier nur kurz ein Problem von PHP-GTK erwähnen. Und zwar kann jeder den auszuführenden PHP Code (also das Programm) frei einsehen … nicht gerade toll! Deshalb bin ich gerade noch dabei ein Script (PHP) zu schreiben, welches eben dieses Problem löst. Und zwar mit einer Verschlüsselung … man nehme den bat to exe converter – schreibe einen key als argv argument von der bat also dann exe datei ein und dann wird es anhand dieses keys, der nicht einsehbar ist, dank .exe Datei während der Laufzeit zurück verschlüsselt und ausgeführt!

    Weiteres erfahrt ihr auf meinem Blog (http://blog.beran-solutions.de/

    Gruß Robert

    Robert Beran

    30 Aug 15 at 16:52

Leave a Reply

You can add images to your comment by clicking here.