Forum Topic: Php: Basic Php-gtk

(1,184 views • 9 replies)

This topic is 1 page long.

<< < > >>
None

CyberLemming

Reply To Post Reply & Quote

Posted at: 8/1/06 08:05 AM

CyberLemming EVIL LEVEL 05

Sign-Up: 08/09/05

Posts: 224

PHP: Main - A collection of PHP tutorials!

PHP-GTK: The basics
You'll need a basic understanding of php at the least, and some knowledge of classes and objects might be a good idea too.

// Foreword //
PHP-GTK is the PHP team's efforts to to package GTK functionality into PHP. GTK is a libary for creation of applications with Graphical User Interfaces (GUIs). The project is still under development, and PHP-GTK2 is still in alpha release.(As of time of writing.) This tutorial is about the basics of the PHP-GTK2 system. Go here for a reference of objects and stuff.

// Preparation //
You will need to download PHP-GTK2 from gtk.php.net, and a release of PHP that works with it from php.net. As of this time of writing, PHP 5.1 is in GA (Generally Available) release, so if you want php 5.2 head over to snaps.php.net. Stick the php release somewhere, and the php-gtk2 somewhere too. Now place the php.exe (and php-win.exe) in your php-gtk dir, along with php5ts.dll (for windows; i'm not sure what it is on linux). Next, if on windows, add (path to phpgtk2)\gtk+2.6.9 to your PATH. (control panel > system > advanced > environment variables > path > edit > add ";(path to phpgtk2)\gtk+2.6.9;" to the end > OK).

// And begin! //
// App 1 - Hello World! //
We're going to make a nice Hello World app =)
create hello.php (or whatever) in your phpgtk dir. open ye php brackets, and place this snippet.

if (!class_exists('gtk'))
die('Please load the php-gtk2 module in your php.ini.');

this simply checks for the existence of gtk.
next, we start our gtk stuff.

$window = new GtkWindow();
$window->connect_simple('destroy', array('Gtk', 'main_quit'));
$window->set_title('Hello World!');

the connect_simple makes the destroying of the window quit the app.

$helloLabel = new GtkLabel('Just popping up to say "Hello World!"');
$window->add($helloLabel);

this code creates a GtkLabel object, which is basically just some text, and adds the label to the window.

$window->show_all();

this simply makes everything show up..

Gtk::main();

this runs the gtk loop and makes the magic happen :D

Complete program:

<?php
if (!class_exists('gtk'))
die('Please load the php-gtk2 module in your php.ini.');
$window = new GtkWindow();
$window->set_title('Hello World!');
$helloLabel = new GtkLabel('Just popping up to say "Hello World!"');
$window->add($helloLabel);
$window->show_all();
Gtk::main();
?>

// App 2 - ROT13 thingy //
Here, we're going to make a small app that lets the user enter whatever into a textbox, and hit a button to rot13 it.
start with the gtk checker, and init` your window and give it a title. don't forget the connect_simple either!

next, we create the objects we need.

$textBox = new GtkEntry();
$button = new GtkButton('_Rot13');
$button->connect_simple('clicked','rot13te
xt',$textbox);
$vBox = new GtkVBox();

GtkEntry is basically a textbox. A GtkButton is what you think it is. The underscore (_) before the R doesn't actually appear in the button, instead it makes the R underlined in the button, and gives the button a hotkey of ALT-R. The connect_simple makes the clicking of the button trigger a function called rot13text (which we shall define) with the parameter $textbox.
a GtkVBox is a simple container widget that will contain our entry field and button - a GtkWindow is a bin not a container, and can only hold one widget on it's own.

$vBox->pack_start($textbox);
$vBox->pack_start($button);

this places our textbox into the vBox, followed by our button..

$window->add($vBox);
$window->show_all();
Gtk::main();

now we also need to define our function.

function rot13text($textbox)
{
$textbox->set_text(str_rot13($textbox->get
_text()));
}

a complete no-brainer =)

Complete program:

<?php
if (!class_exists('gtk'))
die('Please load the php-gtk2 module in your php.ini.');

function rot13text($textbox)
{
$textbox->set_text(str_rot13($textbox->get
_text()));
}

$window = new GtkWindow();
$window->connect_simple('destroy', array('gtk', 'main_quit'));
$window->set_title('Rot13');

$textbox = new GtkEntry();
$button = new GtkButton('_Rot13');
$button->connect_simple('clicked','rot13te
xt',$textbox);
$vBox = new GtkVBox();

$vBox->pack_start($textbox);
$vBox->pack_start($button);

$window->add($vBox);

$window->show_all();

Gtk::main();
?>

// App 2a - ROT13 Thingy with dialog box //

The previous app replaced the text in the textbox. We'll improve it by instead popping up a dialog box.
in the line where we connect clicking $button, add another parameter after $textbox: $window.
now go to our rot13text() functionL replace it with this.

$dialog = new GtkMessageDialog($window, Gtk::DIALOG_MODAL,Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK);
$dialog->set_markup(str_rot13($textbox->ge
t_text()));
$dialog->run();
$dialog->destroy();

GtkMessageDialog is just a convienience widget that wraps GtkDialog. Gtk:MESSAGE_INFO tells it that it's an informational dialog.
the rest, you can work out :P

Complete program:

<?php
if (!class_exists('gtk'))
die('Please load the php-gtk2 module in your php.ini.');

function rot13text($window,$textbox)
{
$dialog = new GtkMessageDialog($window, Gtk::DIALOG_MODAL,Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK);
$dialog->set_markup(str_rot13($textbox->ge
t_text()));
$dialog->run();
$dialog->destroy();
}

$window = new GtkWindow();
$window->connect_simple('destroy', array('gtk', 'main_quit'));
$window->set_title('Rot13');

$textbox = new GtkEntry();
$button = new GtkButton('_Rot13');
$button->connect_simple('clicked','rot13te
xt',$window,$textbox);
$vBox = new GtkVBox();

$vBox->pack_start($textbox);
$vBox->pack_start($button);

$window->add($vBox);

$window->show_all();

Gtk::main();
?>

That's all folks! :D
feedback appreciated.


None

Jordan

Reply To Post Reply & Quote

Posted at: 8/1/06 08:09 AM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,837

A nice tutorial, to bad my host won't have GTK.


None

CyberLemming

Reply To Post Reply & Quote

Posted at: 8/1/06 08:13 AM

CyberLemming EVIL LEVEL 05

Sign-Up: 08/09/05

Posts: 224

At 8/1/06 08:09 AM, -Jordan- wrote: A nice tutorial, to bad my host won't have GTK.

sarcasm isn't appreciated.


None

Jordan

Reply To Post Reply & Quote

Posted at: 8/1/06 08:27 AM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,837

At 8/1/06 08:13 AM, CyberLemming wrote: sarcasm isn't appreciated.

What?


None

CyberLemming

Reply To Post Reply & Quote

Posted at: 8/1/06 08:39 AM

CyberLemming EVIL LEVEL 05

Sign-Up: 08/09/05

Posts: 224

At 8/1/06 08:27 AM, -Jordan- wrote: What?

GTK is for desktop applications. Not web apps.


None

Jordan

Reply To Post Reply & Quote

Posted at: 8/1/06 08:43 AM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,837

At 8/1/06 08:39 AM, CyberLemming wrote: GTK is for desktop applications. Not web apps.

Sorry for being a dumbass *slaps self*.


None

NinoGrounds

Reply To Post Reply & Quote

Posted at: 8/1/06 02:26 PM

NinoGrounds LIGHT LEVEL 17

Sign-Up: 11/28/05

Posts: 3,529

At 8/1/06 08:43 AM, -Jordan- wrote:
At 8/1/06 08:39 AM, CyberLemming wrote: GTK is for desktop applications. Not web apps.
Sorry for being a dumbass *slaps self*.

Lol.

Thanks for this tutorial.


None

Pilot-Doofy

Reply To Post Reply & Quote

Posted at: 8/1/06 03:27 PM

Pilot-Doofy LIGHT LEVEL 37

Sign-Up: 09/13/03

Posts: 12,282

PHP: Main
--------------
Your resource for great PHP tutorials!!!

PHP: GENERAL CODE:

BEGINNER (11)
PHP: Basic Superglobals by SpamBurger
PHP: Beginners tutorial by GamesCool
PHP: Constants by SpamBurger
PHP: Email Form / Contact by MihaS
PHP: Loops by Nino_JoJ
PHP: Managing IP addresses by -Calum-
PHP: Page ID by ElementalFlash
PHP: Sessions and Cookies by WoogieNoogie
PHP: Setting and Returning Cookies by Jams44
PHP: Storing Cookie Info by Jams44
PHP: Website Progress/Percentages by Greeley

INTERMEDIATE (16)
PHP: Basic Ftp Interactions by Pilot-Doofy
PHP: Capitalize Every Word (arrays) by juraj
PHP: Changing Content-types by Pilot-Doofy
PHP: Counting unique hits by Rellizate
PHP: defining functions and stuff by Pilot-Doofy * NEW *
PHP: File uploads by Pilot-Doofy
PHP: Good habits/Security concerns by Sir-Davey
PHP: Image Resizing by Pilot-Doofy
PHP: replace_tags() by Pilot-Doofy
PHP: resizing and copying images by Pilot-Doofy
PHP: RSS() by Deleted user
PHP: Secure PHP Navigation by bigftballjock
PHP: securing file uploads by Pilot-Doofy
PHP: SQL and PHP by SpamBurger
PHP: Starting With Gd by -cherries-
PHP: Using explode and implode functions by GamesCool

ADVANCED (3)
PHP: Advanced Misc by Nino_JoJ
PHP: CLI (Command Line Interface) Basics by Craige
PHP: Function strFind() by Pilot-Doofy
PHP: Basic PHP-GTK by CyberLemming * NEW *

PHP: SPECIFIC PROJECTS:

BEGINNER (8)
PHP: Count Line Breaks by Zendra
PHP: Creating A Random Ad Script by Jams44
PHP: Create a random quotes script by GamesCool
PHP: Date Displayer by Jams44
PHP: Link Checking by Jams44
PHP: Shoutbox (MySQL) by WoogieNoogie
PHP: Simple Hit Counter by SpamBurger
PHP: Voting System (From 1 to 5) by Greeley
PHP: Basic Image Host by -Jorden- * NEW *

-----------------------

Sorry for the late update guys, I'm contacting a mod to get the other posts deleted. I was shopping because stores have started the tax-free week early here. :)

holy jesus what are these goddamn animals


None

Pilot-Doofy

Reply To Post Reply & Quote

Posted at: 8/1/06 03:29 PM

Pilot-Doofy LIGHT LEVEL 37

Sign-Up: 09/13/03

Posts: 12,282

Goddammit, wrong topic. Sorry guys.

holy jesus what are these goddamn animals


None

Craige

Reply To Post Reply & Quote

Posted at: 8/16/06 03:42 PM

Craige LIGHT LEVEL 08

Sign-Up: 07/17/04

Posts: 3,070

Pretty nice tutorial. One of these days I am going to reinstall PHP-GTK and continue working with it. It has been a while for me.


All times are Eastern Standard Time (GMT -5) | Current Time: 08:59 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!