Hello There, You have an OS
Simple application based off 'Hello World' to report the current operating system.
Topic: GtkWindow (View All Tutorials)
Keywords: gtkwindow gtklabel gtkbutton gtkvbox pack markup position
Updated: A Long Time Ago, 2007/11/14 20:58
This is a simple application not much more complex than the Hello World example. It displays a window with your current operating system information on it, along with a button to close it. I use this program to test my PHP-GTK installations.
KateOS 3.6

Ubuntu 7.10

Windows XP

FreeBSD 6.2

Solaris 10

The Application
#!/usr/bin/php -c/etc/gtk/php.ini
<?php
define('THE_STRING_FMT',"<span size='x-large' weight='bold'>Hello There.</span>\nYour Operating System Is...\n\t%s");
if(strtolower(substr(PHP_OS,0,3)) == 'win') {
//. windows...
define('THE_STRING',sprintf(
THE_STRING_FMT,
trim(shell_exec('ver'))
));
} else {
if(trim(shell_exec('which uname'))) {
//. linux bsd solaris...
define('THE_STRING',sprintf(
THE_STRING_FMT,
trim(shell_exec('uname -sr'))
));
}
else {
//. ...
define('THE_STRING',sprintf(
THE_STRING_FMT,
'... something. I have no idea.'
));
}
}
class bobWindow extends GtkWindow {
public $vbox;
public $label,$button;
public function __construct() {
parent::__construct();
$this->vbox = new GtkVBox;
$this->button = new GtkButton('Close');
$this->label = new GtkLabel;
// note 1
$this->label->set_markup(THE_STRING);
$this->set_size_request(300,200);
$this->set_title('Hello There');
$this->set_position(Gtk::WIN_POS_CENTER);
$this->connect_simple('delete-event',array($this,'on_quit'));
// note 2
$this->button->connect_simple('clicked',array($this,'on_quit'));
$this->vbox->pack_start($this->label,true,true,3);
$this->vbox->pack_start($this->button,false,true,0);
$this->add($this->vbox);
$this->show_all();
return;
}
public function on_quit() {
$this->hide();
Gtk::main_quit();
return;
}
}
$window = new bobWindow;
Gtk::main();
$window->destroy();
unset($window);
?>
Note 1
The string which we send to the label you might have noticed has HTML style tags in it. This is actually called Pango Markup and only looks similar to HTML. It allows you to do simple things like adjust the font size and things. To enable the use of markup we use the set_markup() method on GtkLabel instead of passing the text to the constructor or set_text().
Note 2
I connected the same method used to terminate the program when you click the X
to the button I created and packed into the window.
End Game