Introduction
As for PHP, Web is already known as a server-side language for creating applications, PHP-GTK2 and make use of the extension, PHP allows you to create applications that run on the desktop. In this paper, the PHP-GTK2 try to create a simple calculator for desktop use.
Audience
If you want to create PHP applications on the desktop
Environmental Building
Target OS is, Linux and Windows.
PHP-GTK2 is, PHP-GTK can be downloaded for free from our site.
For Windows, “php-gtk-2.**.**-win32-nts.zip” to download and unzip the file.
Basic configuration is at least close.
Sample program “Simple Calculator”
Then use the PHP-GTK2, let’s create a simple calculator. The following shows the source.
File name: calculator.php
<? php
class Calc (
private $ txt_1;
private $ txt_2;
private $ txt_3;
private $ tblTable;
private $ hbox_1;
private $ wnd1;
private $ cboCBox;
private $ calc_btn;
public function __construct () (
/ / Initialize the text box for numeric input
$ this-> txt_1 = new GtkEntry ();
$ this-> txt_2 = new GtkEntry ();
/ / Initialize display box solution for
$ this-> txt_3 = new GtkEntry ();
/ / Adjust the width of each text box,
$ this-> txt_1-> set_width_chars (6);
$ this-> txt_2-> set_width_chars (6);
$ this-> txt_3-> set_width_chars (6);
/ / Initialize the cboCBox box selection operator
$ this-> cboCBox = GtkcboCBoxBox:: new_text ();
$ this-> cboCBox-> insert_text (0 ,"+");
$ this-> cboCBox-> insert_text (1 ,"-");
$ this-> cboCBox-> insert_text (2 ,"×");
$ this-> cboCBox-> insert_text (3 ,"÷");
Appear to //"+" initial
$ this-> cboCBox-> set_active (0);
/ / Initialize the calculation button
$ this-> calc_btn = new GtkButton ("=");
/ / Assign the event listener
$ this-> calc_btn-> connect_simple ( "clicked", array ($ this, "calcHandler"));
/ / Initialize the table
$ this-> tblTable = new GtkTable (1, 5);
/ / Place the components on the table
$ this-> tblTable-> attach ($ this-> txt_1, 0,1,0,1);
$ this-> tblTable-> attach ($ this-> cboCBox, 1,2,0,1);
$ this-> tblTable-> attach ($ this-> txt_2, 2,3,0,1);
$ this-> tblTable-> attach ($ this-> calc_btn, 3,4,0,1);
$ this-> tblTable-> attach ($ this-> txt_3, 4,5,0,1);
$ this-> hbox_1 = new Gtkhbox_1 ();
$ this-> hbox_1-> pack_start ($ this-> tblTable);
$ this-> wnd1 = new GtkWindow ();
$ this-> wnd1-> add ($ this-> hbox_1);
$ this-> wnd1-> set_title ( "PHP Calculator");
$ this-> wnd1-> show_all ();
Gtk:: main ();
)
public function calcHandler () (
$ num1 = doubleval ($ this-> txt_1-> get_text ());
$ num2 = doubleval ($ this-> txt_2-> get_text ());
switch ($ this-> cboCBox-> get_active ()) (
/ / Add
case 0:
$ this-> txt_3-> set_text ($ num1 + $ num2);
break;
/ / Subtraction
case 1:
$ this-> txt_3-> set_text ($ num1 - $ num2);
break;
/ / Multiplication
case 2:
$ this-> txt_3-> set_text ($ num1 * $ num2);
break;
/ / Division default:
$ this-> txt_3-> set_text ($ num1 / $ num2);
)
)
)
/ / Calc, create an instance of the class
$ calc = new Calc ();
?> |
Run the application
Let’s run this application. Open a command prompt, when you could just download the folder php-gtk2. Then, please run the following command:
C:\php-gtk2> php.exe calculator.php |
Result

If you want to quit the application, or applications [×] button on the command line [Ctr] + [C] Please run the shortcut key.
Commentary
Calc components are defined in the class.
Components used in this
GtkEntry Text box.
GtkComboBox Combo box.
GtkButton Button.
GtkTable Table. Placing a child in a grid component.
GtkHBox Horizontal box to hold the table.
GtkWindow Window to store the horizontal box.
GtkEntry and GtkComboBox, GtkButton and small components, GtkTable and place it in a container. To create a GtkTable this, new GtkTable(1,5) and a vertical length, the length of the next five and a specified grid. GtkTable to the placement of components, as follows: attach use the function.
$ this-> tblTable-> attach ($ this-> txt_1, 0,1,0,1); |
Then put the components placed GtkHBox GtkTable objects. GtkHBox is the horizontal component of the box. The horizontal box is placed in the following pack_start use the function.
$ this-> hbox_1-> pack_start ($ this-> tblTable); |
In addition, the box GtkHBox add this add to place a GtkWindow object.
$ this-> wnd1-> add ($ this-> hbox_1); |
To set the title of the GtkWindow, set_title function.
$ this-> wnd1-> set_title ( "PHP Calculator"); |
Also, [=] to make the calculation for the function is called when pressing the button below.
$ this-> calc_btn-> connect_simple ( “clicked”, array ($ this, “calcHandler”));
Signal “clicked”, ie when I click “calcHandler” means that the function is called.
calcHandler Let’s look at the contents of the function.
$ num1 = doubleval ($ this-> txt_1-> get_text ());
$ num2 = doubleval ($ this-> txt_2-> get_text ()); |
txt_1, txt_2 the figures obtained in doubleval to quantify explicitly the function, each variable $ $num1 $ $num2 assigns.
Then, the index of the operator is selected in the combo box of the operator get_active Get function.
$ this-> cboCBox-> get_active () |
And in this case, the index
0 addition, if
If a subtraction
If you multiply two
If the three division
Has been set.
Calc Thus we complete the application to generate the last instance of the class.
Conclusion
This time I made a PHP-GTK2 applications running on their desktop calculator use. PHP-GTK in the rich component than what’s used.
PHP is a desktop application that is not made aware of the opportunity that this PHP-GTK2 to even try to challenge?
It may spread to new worlds.
References
Share on Facebook