I tried my hand at learning Tk today. I wanted to make a calculator, and for all intents and purposes I did. Sort of.
I can't tell if it does what it is supposed to because I can't get the information to show up on the "screen." Supposedly there is a way to "refresh" it, but I can't figure it out.
use Tk;
use strict;
use warnings;
my $number=0;
my $total=0;
my $plus=0;
my $minus=0;
my $times=0;
my $divide=0;
my $main = MainWindow -> new;
$main -> minsize ("150", "200");
$main -> title ("Calculator!");
$main -> configure ( -background => '#FAEBD7');
my $menu_bar = $main -> Frame(
-relief => 'ridge',
-borderwidth => '3',
-background => 'black',
)->pack( -side => 'top', -fill => 'x');
my $file_button = $menu_bar -> Menubutton(
-text => 'File',
-activebackground => 'grey',
-background => 'black',
-foreground => 'white',
)->pack( -side => 'left' );
$file_button -> command(
-label => 'No Use',
-activebackground => 'grey');
$file_button -> separator();
$file_button -> command(
-label => 'Exit',
-activebackground => 'grey',
-command => sub{ $main -> destroy});
$file_button -> separator();
my $help_button = $menu_bar -> Menubutton(
-text => 'Help!',
-activebackground => 'grey',
-background => 'black',
-foreground => 'white',
)->pack( -side => 'right');
$help_button -> command(
-label => 'About',
-activebackground => 'grey',
-command => \&about_txt);
$help_button -> command(
-label => 'Help',
-activebackground => 'grey',
-command => \&help_txt);
$help_button -> separator();
my $top = $main -> Frame(
-background => '#FAEBD7',
) -> pack ( -side => 'top', -fill => 'x');
my $screen = $top -> Label(
-text => "$number",
-background => 'white',
-width => 30,
-borderwidth => 2,
-relief => 'sunken',
) -> pack(-pady => 5);
my $left_num = $top -> Frame(
-background => '#FAEBD7',
-width => 50,
-height => 200,
) -> pack (-side => 'left', -padx => 5, -pady => 2);
my $center_num = $top -> Frame(
-background => '#FAEBD7',
-width => 50,
-height => 200,
) -> pack (-side => 'left', -padx => 5, -pady => 2);
my $right_num = $top -> Frame(
-background => '#FAEBD7',
-width => 50,
-height => 200,
) -> pack (-side => 'left', -padx => 5, -pady => 2);
my $operators_1 = $top -> Frame(
-background => '#FAEBD7',
-width => 50,
-height => 200,
) -> pack (-side => 'left', -padx => 5, -pady => 2);
my $operators_2 = $top -> Frame(
-background => '#FAEBD7',
-width => 50,
-height => 200,
) -> pack (-side => 'left', -padx => 5, -pady => 2);
my $operators_3 = $top -> Frame(
-background => '#FAEBD7',
-width => 50,
-height => 200,
) -> pack (-side => 'left', -padx => 5, -pady => 5);
my $one = $left_num -> Button(
-text => '1',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&one,
) -> pack();
my $two = $center_num -> Button(
-text => '2',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&two,
) -> pack();
my $three = $right_num -> Button(
-text => '3',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&three,
) -> pack();
my $four = $left_num -> Button(
-text => '4',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&four,
) -> pack();
my $five = $center_num -> Button(
-text => '5',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&five,
) -> pack();
my $six = $right_num -> Button(
-text => '6',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&six,
) -> pack();
my $seven = $left_num -> Button(
-text => '7',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&seven,
) -> pack();
my $eight = $center_num -> Button(
-text => '8',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&eight,
) -> pack();
my $nine = $right_num -> Button(
-text => '9',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&nine,
) -> pack();
my $zero = $left_num -> Button(
-text => '0',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&zero,
) -> pack();
my $Plus = $operators_1 -> Button(
-text => '+',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&plus,
) -> pack();
my $Minus = $operators_1 -> Button(
-text => '-',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&minus,
) -> pack();
my $Times = $operators_2 -> Button(
-text => '*',
-background => 'grey',
-width => 5,
-height => 2,
-command => \×,
) -> pack();
my $Divide = $operators_2 -> Button(
-text => '/',
-background => 'grey',
-width => 5,
-height => 2,
-command => \÷,
) -> pack();
my $dec = $center_num -> Button(
-text => '.',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&dec,
) -> pack();
my $equals = $right_num -> Button(
-text => '=',
-background => 'grey',
-width => 5,
-height => 2,
-command => \&equals,
) -> pack();
MainLoop;
sub plus{
$plus=1;
$minus=0;
$times=0;
$divide=0;
1;
}
sub minus{
$plus=0;
$minus=1;
$times=0;
$divide=0;
1;
}
sub times{
$plus=0;
$minus=0;
$times=1;
$divide=0;
1;
}
sub divide{
$plus=0;
$minus=0;
$times=0;
$divide=1;
1;
}
sub dec{
if($number!~m/\./g){
$number .= '.' if $number!=0;
$number = '0.' if $number==0;
}
}
sub one{
$number .= "1" if $number!=0;
$number=1 if $number==0;
if( $plus==0 &&
$minus==0 &&
$times==0 &&
$divide==0){
$total=1;
}
1;
}
sub two{
$number .= "2" if $number!=0;
$number=2 if $number==0;
if( $plus==0 &&
$minus==0 &&
$times==0 &&
$divide==0){
$total=2;
}
1;
}
sub three{
$number .= "3" if $number!=0;
$number=3 if $number==0;
if( $plus==0 &&
$minus==0 &&
$times==0 &&
$divide==0){
$total=3;
}
1;
}
sub four{
$number .= "4" if $number!=0;
$number=4 if $number==0;
if( $plus==0 &&
$minus==0 &&
$times==0 &&
$divide==0){
$total=4;
}
1;
}
sub five{
$number .= "5" if $number!=0;
$number=5 if $number==0;
if( $plus==0 &&
$minus==0 &&
$times==0 &&
$divide==0){
$total=5;
}
1;
}
sub six{
$number .= "6" if $number!=0;
$number=6 if $number==0;
if( $plus==0 &&
$minus==0 &&
$times==0 &&
$divide==0){
$total=6;
}
1;
}
sub seven{
$number .= "7" if $number!=0;
$number=7 if $number==0;
if( $plus==0 &&
$minus==0 &&
$times==0 &&
$divide==0){
$total=7;
}
1;
}
sub eight{
$number .= "8" if $number!=0;
$number=8 if $number==0;
if( $plus==0 &&
$minus==0 &&
$times==0 &&
$divide==0){
$total=8;
}
1;
}
sub nine{
$number .= "9" if $number!=0;
$number=9 if $number==0;
if( $plus==0 &&
$minus==0 &&
$times==0 &&
$divide==0){
$total=9;
}
1;
}
sub zero{
$number .= "0" if $number!=0;
$number=0 if $number==0;
if( $plus==0 &&
$minus==0 &&
$times==0 &&
$divide==0){
$total=0;
}
1;
}
sub equals{
$number = $total + $number if $plus==1;
$number = $total - $number if $minus==1;
$number = $total * $number if $times==1;
$number = $total / $number if $divide==1;
1;
}