Important alert: (current site time 7/15/2013 9:29:07 PM EDT)
 

VB icon

A Multi Temp Converter (Perl/Tk)

Email
Submitted on: 11/5/2003 6:15:21 AM
By: Randy McCleary 
Level: Intermediate
User Rating: By 5 Users
Compatibility: Active Perl specific
Views: 16326
(About the author)
 
     This is a simple Perl/Tk application that acts as a temperature converter between 5 different methods of measurement. So you can convert back and forth.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: A Multi Temp Converter (Perl/Tk)
= Description:This is a simple Perl/Tk application that
acts as a temperature converter between
5 different methods of measurement. So you can convert back and forth.
= By: Randy McCleary
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=549&lngWId=6=for details.=**************************************

#!/usr/bin/perl -w
############################################
## This is a simple Perl/Tk application that
##acts as a temperature converter between
##5 different methods of measurement.
############################################
use strict;
use Tk;
use Tk::Dialog;
use Tk::DialogBox;
use Number::Format qw(:subs);
require Tk::LabFrame;
	
my $from;
my $to;
	
	
my $frmMain = MainWindow->new;
$frmMain->title("Temperature Converter");
$frmMain->geometry("447x334+8+8");
	
	
####################################################################
## Here we are going to create to Label Frames with the labels
## placed across the top for the Convert From and Convert To
####################################################################		
my $fraConvertFrom = $frmMain->LabFrame(
 				-borderwidth => '1',
 				-label => "Convert From",
 				-labelside => "acrosstop",
 				-relief => 'flat');
 
my $fraConvertTo = $frmMain->LabFrame(
 				-borderwidth => '1',
 				-label => "Convert To",
 				-labelside => "acrosstop",
 				-relief => 'flat');
 				
####################################################################
## Now we are going to set up the Radio buttons inside of the first
## frame which is the Convert From frame.
####################################################################
my $from_celsius = $fraConvertFrom->Radiobutton(
 				-activeforeground => '#0000FF',
 				-anchor=> 'w',
 				-font => "Arial 9 normal",
 				-relief => 'flat',
 				-text => 'Celsius',
 				-relief => 'flat',
 				-value=> '1',
 				-variable => \$from
 				)->pack;
	
my $from_fahrenheit = $fraConvertFrom->Radiobutton(
 				-activeforeground => '#0000FF',
 				-anchor=> 'w',
 				-font => "Arial 9 normal",
 				-relief => 'flat',
 				-text => 'Fahrenheit',
 				-value=> '2',
 				-variable => \$from
 				)->pack;	
	
my $from_kelvin = $fraConvertFrom->Radiobutton(
 				-activeforeground => '#0000FF',
 				-anchor=> 'w',
 				-font => "Arial 9 normal",
 				-relief => 'flat',
 				-text => 'Kelvin',
 				-value=> '3',
 				-variable => \$from
 				)->pack;
	
my $from_rankine = $fraConvertFrom->Radiobutton(
 				-activeforeground => '#0000FF',
 				-anchor=> 'w',
 				-font => "Arial 9 normal",
 				-relief => 'flat',
 				-text => 'Rankine',
 				-value=> '4',
 				-variable => \$from
 				)->pack;
	
my $from_reaumur = $fraConvertFrom->Radiobutton(
 				-activeforeground => '#0000FF',
 				-anchor=> 'w',
 				-font => "Arial 9 normal",
 				-relief => 'flat',
 				-text => 'Reaumur',
 				-value=> '5',
 				-variable => \$from
 				)->pack;			 				 				
 				
$from_celsius->place(
		 -width => 96, 
		 -height => 24, 
		 -x => 8, 
		 -y => 8); 				 							
	
$from_fahrenheit->place(
		 -width => 96, 
		 -height => 24, 
		 -x => 8, 
		 -y => 32);
	
$from_kelvin->place(
		 -width => 96, 
		 -height => 24, 
		 -x => 8, 
		 -y => 57);
	
$from_rankine->place(
		 -width => 96, 
		 -height => 24, 
		 -x => 8, 
		 -y => 81);
	
$from_reaumur->place(
		 -width => 96, 
		 -height => 24, 
		 -x => 8, 
		 -y => 105);								
####################################################################
	
####################################################################
## Now we are going to set up the next Radio buttons for the 
## Convert To frame.
####################################################################
my $to_celsius = $fraConvertTo->Radiobutton(
 				-activeforeground => '#0000FF',
 				-anchor=> 'w',
 				-font => "Arial 9 normal",
 				-relief => 'flat',
 				-text => 'Celsius',
 				-value=> '1',
 				-variable => \$to
 				)->pack;
	
my $to_fahrenheit = $fraConvertTo->Radiobutton(
 				-activeforeground => '#0000FF',
 				-anchor=> 'w',
 				-font => "Arial 9 normal",
 				-relief => 'flat',
 				-text => 'Fahrenheit',
 				-value=> '2',
 				-variable => \$to
 				)->pack;	
	
my $to_kelvin = $fraConvertTo->Radiobutton(
 				-activeforeground => '#0000FF',
 				-anchor=> 'w',
 				-font => "Arial 9 normal",
 				-relief => 'flat',
 				-text => 'Kelvin',
 				-value=> '3',
 				-variable => \$to
 				)->pack;
	
my $to_rankine = $fraConvertTo->Radiobutton(
 				-activeforeground => '#0000FF',
 				-anchor=> 'w',
 				-font => "Arial 9 normal",
 				-relief => 'flat',
 				-text => 'Rankine',
 				-value=> '4',
 				-variable => \$to
 				)->pack;
	
my $to_reaumur = $fraConvertTo->Radiobutton(
 				-activeforeground => '#0000FF',
 				-anchor=> 'w',
 				-font => "Arial 9 normal",
 				-relief => 'flat',
 				-text => 'Reaumur',
 				-value=> '5',
 				-variable => \$to
 				)->pack;			 				 				
 				
$to_celsius->place(
		 -width => 96, 
		 -height => 24, 
		 -x => 8, 
		 -y => 8); 				 							
	
$to_fahrenheit->place(
		 -width => 96, 
		 -height => 24, 
		 -x => 8, 
		 -y => 32);
	
$to_kelvin->place(
		 -width => 96, 
		 -height => 24, 
		 -x => 8, 
		 -y => 57);
	
$to_rankine->place(
		 -width => 96, 
		 -height => 24, 
		 -x => 8, 
		 -y => 81);
	
$to_reaumur->place(
		 -width => 96, 
		 -height => 24, 
		 -x => 8, 
		 -y => 105);
####################################################################
	
$fraConvertFrom->pack;
$fraConvertTo->pack;
	
##########################################
## Here is where we set the placement of
##the two LabelFrames.
##########################################
$fraConvertFrom->place(
		 -width => 192, 
		 -height => 176, 
		 -x => 24, 
		 -y => 16);
	
$fraConvertTo->place(
		 -width => 192, 
		 -height => 176, 
		 -x => 232, 
		 -y => 16);
		 
####################################################################
my $lblEnter = $frmMain->Label(
 				-anchor => "w", 
 				-text => "Enter Value to Convert:", 
 				-background => "#D4D0C8", 
 				-cursor => "", 
 				-font => "Arial 9 normal", 
 				-relief => "flat"
 				);
	
my $lblConvert = $frmMain->Label(
 				-anchor => "w", 
 				-text => "Converted Value:", 
 				-background => "#D4D0C8", 
 				-cursor => "", 
 				-font => "Arial 9 normal", 
 				-relief => "flat"
 				);
 				
my $txtInput = $frmMain->Entry(
 				-cursor => "", 
 				-font => "Tahoma 8 normal",
 				-justify => "right"
 				);
	
my $txtValue = $frmMain->Entry(
 				-cursor => "", 
 				-font => "Tahoma 8 normal",
 				-justify => "right"
 				);
 				
	
$lblEnter->place(
		 -width => 144, 
		 -height => 24, 
		 -x => 32, 
		 -y => 200);
	
$lblConvert->place(
		 -width => 152, 
		 -height => 24, 
		 -x => 240, 
		 -y => 200);
	
$txtInput->place(
		 -width => 152, 
		 -height => 24, 
		 -x => 32, 
		 -y => 232);
	
$txtValue->place(
		 -width => 152, 
		 -height => 24, 
		 -x => 240, 
		 -y => 232);
	
####################################################################
my $cmdConvert = $frmMain->Button(
 				-activeforeground => "#000000",
 				-background => "#FFFFFF",
 				-borderwidth => 1,
 				-text => "Convert", 
 				-command => sub{Convert()}, 
 				-cursor => "", 
 				-font => "Arial 10 bold", 
 				-foreground => "#820000",
 				-relief => "solid"
 				);
	
my $cmdExit = $frmMain->Button(
 				-activeforeground => "#000000",
 				-background => "#FFFFFF",
 				-borderwidth => 1,
 				-text => "Exit", 
 				-command => [$frmMain => 'destroy'], 
 				-cursor => "", 
 				-font => "Arial 10 bold", 
 				-foreground => "#820000",
 				-relief => "solid"
 				);
 
 				
$cmdConvert->place(
		 -width => 120, 
		 -height => 40, 
		 -x => 40, 
		 -y => 272);
	
$cmdExit->place(
		 -width => 115, 
		 -height => 41, 
		 -x => 248, 
		 -y => 272);
		 		 
MainLoop;
###################################################################### 
## Subs Functions below
###################################################################### 
	
sub Convert {
	my $question = $txtInput->get;
	my $ans;
	
	if ($from == '1') {
		if ($to == '1') {
			$ans = $question;
		}
		elsif ($to == '2') {
			$ans = ($question * 1.8) + 32;
		}
		elsif ($to == '3') {
			$ans = $question + 273.16;
		}
		elsif ($to == '4') {
			$ans = ($question + 273.16) * 1.8;
		}
		elsif ($to == '5') {
			$ans = $question / 1.25;
		}
	}
	elsif ($from == '2') {
		if ($to == '1') {
			$ans = ($question - 32) / 1.8;
		}
		elsif ($to == '2') {
			$ans = $question;
		}
		elsif ($to == '3') {
			$ans = ($question + 459.67) / 1.8;
		}
		elsif ($to == '4') {
			$ans = $question + 459.67;
		}
		elsif ($to == '5') {
			$ans = ($question - 273.16) / 1.25;
		}
	}
	elsif ($from == '3') {
		if ($to == '1') {
			$ans = $question - 273.16;
		}
		elsif ($to == '2') {
			$ans = ($question * 1.8) - 459.67;
		}
		elsif ($to == '3') {
			$ans = $question;
		}
		elsif ($to == '4') {
			$ans = $question * 1.8;
		}
		elsif ($to == '5') {
			$ans = ($question - 273.16) / 1.25;
		}
	}
	elsif ($from == '4') {
		if ($to == '1') {
			$ans = ($question / 1.8) - 273.16;
		}
		elsif ($to == '2') {
			$ans = $question - 459.67;
		}
		elsif ($to == '3') {
			$ans = $question / 1.8;
		}
		elsif ($to == '4') {
			$ans = $question;
		}
		elsif ($to == '5') {
			$ans = (($question / 1.8) - 273.16) / 1.25;
		}
	}
	elsif ($from == '5') {
		# easy way out: first convert to kelvin
		my $kelvin = ($question * 1.25) + 273.16;
		if ($to == '1') {
			$ans = $kelvin - 273.16;
		}
		elsif ($to == '2') {
			$ans = ($kelvin * 1.8) - 459.67;
		}
		elsif ($to == '3') {
			$ans = $kelvin;
		}
		elsif ($to == '4') {
			$ans = $kelvin * 1.8;
		}
		elsif ($to == '5') {
			$ans = ($kelvin - 273.16) / 1.25;
		}
	}
## Set Display Text to Normal 
$txtValue ->configure(-state => "normal");
$ans = format_number($ans, 2, 1);
	$txtValue->delete('0', 'end');
	$txtValue->insert('0', $ans);
	my @scales = ("degrees Celsius", "degrees Fahrenheit", "Kelvin", "degrees Rankine", "degrees Reaumur");
	my $dialog = $frmMain->Dialog(
 				-title => "Temperature Converter",
 				-bitmap => 'question',
 				-text => "$question $scales[$from-1] is $ans $scales[$to-1]",
 				-buttons => ["OK"]);
	$dialog->Show();
	
	## Set Display Text Value back to disabled
	$txtValue ->configure(-state => "disable");
}
	
	
	
	
	
	
	
	
	
	
	
	
	


Other 28 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/9/2003 3:06:41 PMDamian myerscough

Nice Coding
(If this comment was disrespectful, please report it.)

 
1/2/2005 3:34:29 PM

Excellent. I love the TK layout.
(If this comment was disrespectful, please report it.)

 
3/2/2005 10:16:52 PM

Great code, although Number::format not found in Activevision distribution ... I've been looking in vain for Perl/TK examples OUTSIDE of Python until this. Thanks!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.