#!/usr/bin/perl -w # tempcon version 0.1 for Perl/Tk # by Alan Ford 27/03/1999 # # Description: # Allows conversion of temperatures between different scales # # Features to come: # - Info box # # Distributed with no warranty under the GNU Public License # Since there are 5 different temperature scales, there are 25 # conversions. To simplify, always convert the input to kelvin, # then convert to the output. # This reduces the 25 to 10 (5 in, 5 out). # # Input conversions are: # Kelvin: k = 1.0 * (k + 0.0) # Celcius: k = 1.0 * (c + 273.16) # Fahrenheit: k = 5/9 * (f + 459.688) # Rankine: k = 5/9 * (r + 0.0) # Reaumur: k = (1.25 * R) + 273.16 my %mul = ('k'=>1.0, 'c'=>1.0, 'f'=>5.0/9.0, 'r'=>5.0/9.0, 'R'=>1.25); my %add = ('k'=>0.0, 'c'=>273.16, 'f'=>459.688, 'r'=>0.0, 'R'=>273.16/1.25); my %scales = ('c'=>"degrees Celsius", 'f'=>"degrees Fahrenheit", 'k'=>"Kelvin", 'r'=>"degrees Rankine", 'R'=>"degrees Reaumur"); require 5.002; # use English; use Tk; use Tk::DialogBox; # use strict; sub convert ; my $MW = MainWindow->new; # NOT IMPLEMENTED DUE TO AN ERROR ALWAYS CREATED BY ENTRY BOX!!! # set up a warning handler that displays the warning in a Tk dialog box #BEGIN { # $SIG{__WARN__} = sub { # if (defined $MW) { # my $warnbox = $MW->DialogBox( -title => "Warning", # -buttons => [ "Acknowledge" ]); # $warnbox->add("Label", -text => $_[0])->pack; # $warnbox->Show; # } else { # print STDOUT join("\n", @_), "n"; # } # }; #} $MW->title("Temperature Converter"); $MW->Label(-text => "Version 0.1 - Written by Alan Ford\n") ->pack(-side => 'bottom'); my $ans; my $exit = $MW->Button(-text => 'Exit', -command => sub { #print STDOUT "Goodbye.\n"; exit; }); $exit->pack(-side => 'bottom', -expand => '1', -fill => 'both'); my $convert = $MW->Button(-text => 'Convert', -command => sub { convert; }); $convert->pack(-side => 'bottom', -expand => '1', -fill => 'both'); my $answer_frame = $MW->Frame()->pack(-expand => '1', -fill => 'both', -side => 'bottom'); $answer_frame->Label(-text => "Converted value:")->pack(-side => 'left'); my $answer_value = $answer_frame->Entry(-width => '15', -relief => 'sunken')->pack(-side => 'right'); my $value_frame = $MW->Frame()->pack(-expand => '1', -fill => 'both', -side => 'bottom'); $value_frame->Label(-text => "Enter value to convert:")->pack(-side => 'left'); my $convert_value = $value_frame->Entry(-width => '10', -relief => 'sunken')->pack(-side => 'right'); #$convert_value->bind('' => convert); my $from_frame = $MW->Frame(-relief => 'raised', -width => '100', -height => '200'); $from_frame->pack(-side => 'left', -expand => '1', -fill => 'both'); my $to_frame = $MW->Frame(-relief => 'raised', -width => '100', -height => '200'); $to_frame->pack(-side => 'right', -expand => '1', -fill => 'both'); $from_frame->Label(-text => "Convert From")->pack(); my $from_celsius = $from_frame->Radiobutton(-variable => \$from, -value => 'c', -text => 'Celsius') ->pack(-side => 'top', -anchor => 'w'); my $from_fahrenheit = $from_frame->Radiobutton(-variable => \$from, -value=> 'f', -text => 'Fahrenheit') ->pack(-side => 'top', -anchor => 'w'); my $from_kelvin = $from_frame->Radiobutton(-variable => \$from, -value => 'k', -text => 'Kelvin') ->pack(-side => 'top', -anchor => 'w'); my $from_rankine = $from_frame->Radiobutton(-variable => \$from, -value => 'r', -text => 'Rankine') ->pack(-side => 'top', -anchor => 'w'); my $from_reaumur = $from_frame->Radiobutton(-variable => \$from, -value => 'R', -text => 'Reaumur') ->pack(-side => 'top', -anchor => 'w'); $to_frame->Label(-text => "Convert To")->pack(); my $to_celsius = $to_frame->Radiobutton(-variable => \$to, -value => 'c', -text => 'Celsius') ->pack(-side => 'top', -anchor => 'w'); my $to_fahrenheit = $to_frame->Radiobutton(-variable => \$to, -value => 'f', -text => 'Fahrenheit') ->pack(-side => 'top', -anchor => 'w'); my $to_kelvin = $to_frame->Radiobutton(-variable => \$to, -value => 'k', -text => 'Kelvin') ->pack(-side => 'top', -anchor => 'w'); my $to_rankine = $to_frame->Radiobutton(-variable => \$to, -value => 'r', -text => 'Rankine') ->pack(-side => 'top', -anchor => 'w'); my $to_reaumur = $to_frame->Radiobutton(-variable => \$to, -value => 'R', -text => 'Reaumur') ->pack(-side => 'top', -anchor => 'w'); #set defaults: Centigrade to Fahrenheit $from_celsius->select; $to_fahrenheit->select; MainLoop; #subs here sub convert { my $question = $convert_value->get; # convert to kelvin my $kelvin_value = $mul{$from} * ( $question + $add{$from} ); printf("From $question ($from) to $kelvin_value (k)\n"); # convert to output - just do in reverse my $ans = $kelvin_value / $mul{$to} - $add{$to}; printf("From $question ($from) to $ans ($to)\n"); $answer_value->delete('0', 'end'); $answer_value->insert('0', $ans); my $dialog = $MW->DialogBox( -title => "Temperature Converter", -buttons => [ "OK" ], ); $dialog->add("Label", -text => "$question $scales{$from} is $ans $scales{$to}")->pack; $dialog->Show; }