Calculator in PHP GTK2

|
Free Simple Calculator in php gtk2 with source code. This calculator perform basic arithmetic operations i.e Addition, Subtraction, Multiplication, Division. Hope you like it. Thanks! For reading my Blog.

<?php
/* 
PHP GTK tutorials
website: http://phpgtktutorials.blogspot.in/
*/

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


$wnd = new GtkWindow();
$action = NULL;
$firstValue = NULL;
$result =NULL;

function pressClear(GtkWindow $wnd, GtkEntry $txtEntry, $txtButton)
{
    //clear $txtEntry
    $txtEntry->set_text($txtButton);
    
}

function pressBack(GtkWindow $wnd, GtkEntry $txtEntry)
{
    //backspace
    $txtEntryValue = $txtEntry->get_text();
    $newTxtEntryValue = substr("$txtEntryValue", 0, -1);
    $txtEntry->set_text($newTxtEntryValue);
}


function pressAction(GtkWindow $wnd, GtkEntry $txtEntry, $currentAction, $label, $action)
{

$firstValue = $txtEntry->get_text();
$txtEntry->set_text(NULL);

$label->set_text($firstValue);
$action->set_text($currentAction);


}


function pressEqualto(GtkWindow $wnd, GtkEntry $txtEntry, $action, $label)
{
    $secondValue = $txtEntry->get_text();
    $firstValue = $label->get_text();
    $currentaction = $action->get_text();

    if($currentaction=="+")
    {
        $result = $firstValue+$secondValue;
    }
    elseif ($currentaction=="*") {
        
        $result = $firstValue*$secondValue;
    }
    elseif ($currentaction=="/") {
        
        $result = $firstValue/$secondValue;
    }
    elseif ($currentaction=="-") {
        
        $result = $firstValue-$secondValue;
    }

    $txtEntry->set_text($result);


}



function pressButton(GtkWindow $wnd, GtkEntry $txtEntry, $txtButton)
{
    //get the values from the widgets into variables
    $txtEntryValue = $txtEntry->get_text();
    

    if(empty($txtEntryValue))
    {

        $txtEntry->set_text($txtButton);
        
    }else{

        $txtEntry->set_text($txtEntryValue.$txtButton);

    }    



}



        $wnd->set_title('Calculator');         
        $wnd->connect_simple('destroy', array('gtk', 'main_quit')); 

        $vbox = new GtkVBox(false, 2);

        $mb = new GtkMenubar();
        $filemenu = new GtkMenu();
        $filemi = new GtkMenuItem("File");
        $filemi->set_submenu($filemenu);
        $mb->append($filemi);

        $vbox->pack_start($mb, false, false, 0);

        $table = new GtkTable(5, 4, true);

        // Set up all the widgets we need
        $btnClose = new GtkButton("_Close");

        $btnOne = new GtkButton("1");
        $btnTwo = new GtkButton("2");
        $btnThree = new GtkButton("3");
        $btnFour = new GtkButton("4");
        $btnFive = new GtkButton("5");
        $btnSix = new GtkButton("6");
        $btnSeven = new GtkButton("7");
        $btnEight = new GtkButton("8");
        $btnNine = new GtkButton("9");
        $btnZero = new GtkButton("0");

        $btnClear = new GtkButton("Clear");
        $btnBack = new GtkButton("Clear Entry");

        $btnAddition = new GtkButton("+");
        $btnDivide = new GtkButton("/");
        $btnMinus = new GtkButton("-");
        $btnProduct = new GtkButton("*");
        $btnEqualto = new GtkButton("=");

        $labelHidden = new GtkLabel(); // store firstvalue
        $labelAction = new GtkLabel(); // store operations

        $table->attach($btnClear, 0, 1, 0, 1);
        $table->attach($btnBack, 1, 2, 0, 1);
        @$wnd->set_title($labelHidden);
        @$wnd->set_title($labelAction);

        $table->attach($btnClose, 3, 4, 0, 1);

        $table->attach($btnSeven, 0, 1, 1, 2);
        $table->attach($btnEight, 1, 2, 1, 2);
        $table->attach($btnNine, 2, 3, 1, 2);
        $table->attach($btnDivide, 3, 4, 1, 2);

        $table->attach($btnFour, 0, 1, 2, 3);
        $table->attach($btnFive, 1, 2, 2, 3);
        $table->attach($btnSix, 2, 3, 2, 3);
        $table->attach($btnProduct, 3, 4, 2, 3);

        $table->attach($btnOne, 0, 1, 3, 4);
        $table->attach($btnTwo, 1, 2, 3, 4);
        $table->attach($btnThree, 2, 3, 3, 4);
        $table->attach($btnMinus, 3, 4, 3, 4);

        $table->attach($btnZero, 0, 1, 4, 5);
        $table->attach(new GtkButton("."), 1, 2, 4, 5);
        $table->attach($btnEqualto, 2, 3, 4, 5);
        $table->attach($btnAddition, 3, 4, 4, 5);

        $txtEntry = new GtkEntry();
        $vbox->pack_start($txtEntry, false, false, 0);
        $vbox->pack_end($table, true, true, 0);

        $wnd->add($vbox);        


        // Destroy the window when the user clicks Close button
        $btnClose->connect_simple('clicked', array($wnd, 'destroy'));

        // Clear entry label  when the user clicks Clear button
        $btnClear->connect_simple('clicked', 'pressClear', $wnd, $txtEntry, NULL);

        // BackSpace label  when the user clicks Back button
        $btnBack->connect_simple('clicked', 'pressBack', $wnd, $txtEntry);

        // Call the pressEqualto function 
        $btnEqualto->connect_simple('clicked', 'pressEqualto', $wnd, $txtEntry, $labelAction, $labelHidden);

        // Call the pressAction function when the user clicks on numeric button
        $btnAddition->connect_simple('clicked', 'pressAction', $wnd, $txtEntry, "+", $labelHidden, $labelAction);
        $btnDivide->connect_simple('clicked', 'pressAction', $wnd, $txtEntry, "/", $labelHidden, $labelAction);
        $btnMinus->connect_simple('clicked', 'pressAction', $wnd, $txtEntry, "-", $labelHidden, $labelAction);
        $btnProduct->connect_simple('clicked', 'pressAction', $wnd, $txtEntry, "*", $labelHidden, $labelAction);
 
        // Call the pressButton function when the user clicks on numeric button
        $btnOne->connect_simple('clicked', 'pressButton', $wnd, $txtEntry, "1");
        $btnTwo->connect_simple('clicked', 'pressButton', $wnd, $txtEntry, "2");
        $btnThree->connect_simple('clicked', 'pressButton', $wnd, $txtEntry, "3");
        $btnFour->connect_simple('clicked', 'pressButton', $wnd, $txtEntry, "4");
        $btnFive->connect_simple('clicked', 'pressButton', $wnd, $txtEntry, "5");
        $btnSix->connect_simple('clicked', 'pressButton', $wnd, $txtEntry, "6");
        $btnSeven->connect_simple('clicked', 'pressButton', $wnd, $txtEntry, "7");
        $btnEight->connect_simple('clicked', 'pressButton', $wnd, $txtEntry, "8");
        $btnNine->connect_simple('clicked', 'pressButton', $wnd, $txtEntry, "9");
        $btnZero->connect_simple('clicked', 'pressButton', $wnd, $txtEntry, "0");


        $wnd->set_default_size(300, 250); 
        $wnd->set_position(GTK::WIN_POS_CENTER);
                
 

//Show all widgets
$wnd->show_all();
//Start the main loop
Gtk::main();

?>

0 comments:

Post a Comment