Quick Search for:  in language:    
simple,PerlTk,application,thatacts,temperatur
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 75,356. lines
 Jobs: 26. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for Perl.
Message Sender
By sp on 1/15


Click here to see a screenshot of this code!Mailing List v2.0
By Aaron L. Anderson on 1/7

(Screen Shot)

ShowIMG
By Jeff Mills on 1/5


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

A Multi Temp Converter (Perl/Tk)

Print
Email
 
VB icon
Submitted on: 11/5/2003 6:15:21 AM
By: Randy McCleary 
Level: Intermediate
User Rating: By 4 Users
Compatibility:Active Perl specific

Users have accessed this code 1309 times.
 
(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 a
    =     pplication 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://w
    =     ww.Planet-Source-Code.com/vb/scripts/Sho
    =     wCode.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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 PM:Damian myerscough
Nice Coding
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Perl Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.