use strict;
use warnings;
use diagnostics;
use Tk 800;
use Tk::Entry;
use Tk::Button;
use Tk::DialogBox;
my $calc = '0.00';
my $r = 0;
my $w = '0';
my $h = 1;
my $buffer = '';
my $dbuffer = '';
my %button;
my @rows;
my $mw = new MainWindow(-title => 'Calc');
$mw->geometry("300x300");
my $menubar = $mw->Menu;
$mw->configure(-menu => menuconf());
my $topframe = $mw->Frame(-height => '60', -width => '300')->pack(-sid
+e => 'top', -expand => '0', -fill => 'x');
my $btmframe = $mw->Frame(-height => '225', -width => '300',)->pack(-s
+ide => 'left', -expand => '1', -fill => 'both');
push @rows, $btmframe->Frame()->pack( -expand => 1, -fill => 'both', -
+side => 'top') for (0..3);
my $display = $topframe->Entry(-justify => 'right',
-state => 'disabled',
-textvariable => \$calc)
->pack(-expand => '1',
-fill => 'x',
-pady => 30,
-padx => 20,
-side => 'right');
$mw->bind("<KeyRelease>" , sub { &keypress } );
for my $i (
qw/
7 8 9 * n*
4 5 6 - \/
1 2 3 + =
0 . C / )
{
if($i eq '+' || $i eq '=')
{
$h = 3;
}
$button{$i} = $rows[$r]->Button(-text => "$i",
-width => '3',
-height => "$h",
-command => sub { &btnpress($i) })
->pack(-expand => 1,
-fill => 'both',
-padx => 2,
-pady => 2,
-side => 'left',
-ipadx => 5,
-ipady => 5);
$w++;
$h = 1;
if($w > 4){$w = 0; $r++;}
}
MainLoop;
sub btnpress{
my $btn = shift;
if($btn eq 'n*' && $buffer ne '')
{
$btn = '**';
calc($btn);
}
elsif($btn eq 'C')
{
$calc = '0.00';
$buffer = '';
$dbuffer= '';
}
elsif($btn eq '=')
{
$buffer = eval($buffer);
$calc = sprintf "%0.2f", $buffer;
}
else
{
calc($btn);
}
}
sub calc{
my $btn = shift;
$buffer = $buffer . $btn;
if( $btn =~ /(\d)|\./){ $dbuffer = $dbuffer . $btn }
unless( $btn =~ /(\d)|\./){ $calc = ''; $dbuffer = ''; }
if( $btn =~ /(\d)|\./){ $calc = sprintf "%0.2f", $dbuffer; }
}
sub keypress{
if($_[0] ne 'q'){
my $widget = shift;
my $e = $widget->XEvent; # get event object
my $btn = $e->K;
$btn=~s/period/\./ig;
if( $btn =~/c/ig ){ $btn = uc $btn; $button{$btn}->invoke; }
if( $btn =~/q/ig ){ exit 0; }
if( $btn =~/(\d)|\./){ &btnpress($btn); }
}
else
{
exit 0;
}
}
sub menuconf{
my $file_menu = $menubar->cascade(-label => "~File");
my $func_menu = $menubar->cascade(-label => "~Function");
my $help_menu = $menubar->cascade(-label => "~Help");
$file_menu->command(-label => "~Clear C", -command => sub { \
+&btnpress('C' ); });
$file_menu->command(-label => "~Exit Q", -command => sub { \
+&keypress('q' ); });
$func_menu->command(-label => "~Add +" , -command => sub { \
+&btnpress('+' ); });
$func_menu->command(-label => "~Minus -" , -command => sub { \
+&btnpress('-' ); });
$func_menu->command(-label => "~Multiply x" , -command => sub { \
+&btnpress('x' ); });
$func_menu->command(-label => "~Divide /" , -command => sub { \
+&btnpress('/' ); });
$func_menu->command(-label => "~Power of n*", -command => sub { \
+&btnpress('n*'); });
$func_menu->command(-label => "~Equals =" , -command => sub { \
+&btnpress('=' ); });
$help_menu->command(-label => "~About" , -command => sub { \
+&aboutbox() });
return $menubar;
}
sub aboutbox{
my $about_box = $mw->DialogBox(-title => "About", -buttons => ["OK"])
+;
$about_box->add('Label', -anchor => 'w',
-justify => 'center',
-text => qq(
This Simple Calculator was created Feb. 2004 by Eoin Murphy.
It was completed with the help of http:\/\/www.perlmonks.com, and thei
+r
wonderful members. JamesNC and zentara to mention a few who helped.))-
+>pack(qw/-side top -anchor w/);
$about_box->add('Label', -anchor => 'sw',
-justify => 'left',
-text => qq(
eoin
eoinmurphy00\@hotmail.com
http:\/\/eoin.perlmonk.org))->pack(qw/-side top -anchor sw/);
$about_box->Show();
}
|