Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Perl/Tk - Chemistry::MolecularMass

by PERLscienceman (Curate)
on Aug 06, 2003 at 02:29 UTC ( #281248=CUFP: print w/ replies, xml ) Need Help??

Greetings Fellow Monks! In my continuing quest for cool Perl Modules combined with my ongoing desire to tinker and learn new things in Perl I have found yet another module on CPAN that I found interesting: Chemistry::MolecularMass. I decided to put it to a simple Perl/Tk interface as a companion to a similar post that I assembled: Perl/Tk - Chemistry::Elements. For this interface you enter a Chemical Formula such as H2O as H2O, click on "Calculate" and the module does the rest, outputting the molecular mass in the labeled widget. The documentation on CPAN Chemistry::MolecularMass goes into further detail as to the capabilities of the module in addition to what the module's object will accept as a valid entry. Anyway, enough said presented below is the code.

And As Always, any input or suggestions for improvement of this code are greatly appreciated. Enjoy and Have Fun!

Learning Is What Makes Life Interesting!


#!/usr/bin/perl -w # use English; use Chemistry::MolecularMass; use Tk; use Tk::DialogBox; use strict; sub calculate ; my $mm = new Chemistry::MolecularMass; my $MW = MainWindow->new(-background=>'#DEDE20'); ### --- Prevents Main Window Resizing $MW->bind('<Configure>' => sub{ my $xe = $MW->XEvent; $MW->maxsize($xe->w, $xe->h); $MW->minsize($xe->w, $xe->h); }); ### --- Main Window Title $MW->title("Molecular Mass"); ### --- Create Button Frame my $button_frame = $MW->Frame(-background => '#DEDE20'); $button_frame->pack(-side => 'bottom', -expand => '1', -fill = +> 'both'); ### --- Create About Button $button_frame->Button(-text => "About", -foreground => "#007300", -activebackground => "#98FB98", -background => "#E8F988", -font => 'Arial', -command => sub { my $dialogA = $MW->DialogBox( -title => "About this program: + MolecularMass.pl", -bu +ttons => [ "OK" ], ); $dialogA->add("Label", -borderwidth => '3', -background => "#FAEBD7", #ANTIQUE WHITE -font => 'bold', -justify => 'left', -relief => 'sunken', #One seriously long text string f +ollows on the next line --> -text => "FOR BEST RESULTS USE: PERL 5 +.6.1 \& Tk 8.0 OR BETTER\nThis program requires use of the perl modul +e Chemistry::MolecularMass.\nVersion 1.0 -- 07\/27\/2003 Original +Code Written by Jay Madigan.\n\t Adapted from TheElements.pl +written by Jay Madigan c.2000\n************************************** +**********************************************************\nThis prog +ram is freeware and may be modified and\/or redistributed \nunder the + same terms as the GNU public license of PERL. \nNo warranty on this +code is assumed or implied. ")->pack; ### --- Prevents Dialog Box Resizing $dialogA->bind('<Configure>' => sub{ my $xeD = $dialogA->XEvent; $dialogA->maxsize($xeD->w, $xeD->h); $dialogA->minsize($xeD->w, $xeD->h); }); $dialogA->Show; } )->pack(-side => 'right', -expand => '1', -fill => 'both'); ### --- Create Exit Button my $exit = $button_frame->Button(-text => 'Exit', -foreground => 'red', -activebackground => "#FF9090", -background => "#E8F988", -font => 'Arial', -command => sub { exit; }); $exit->pack(-side => 'right', -expand => '1', -fill => 'both'); ### --- Create Calculate Button my $calculate = $button_frame->Button(-text => 'Calculate', -foreground => "blue", -activebackground => "#ADD8E6", -background => "#E8F988", -font => 'Arial', -command => sub { calculate; }); $calculate->pack(-side => 'right', -expand => '1', -fill => 'both'); ### --- Create Molecular Mass Frame my $mass_frame = $MW->Frame(-background => '#DEDE20')->pack(-expand => + '1', -fill => 'both', -side => 'bottom'); $mass_frame->Label(-background => '#DEDE20',-text => "Molecular Mass:" +, -font => 'Arial')->pack(-side => 'left'); my $mass_value = $mass_frame->Entry(-highlightbackground => 'black',-h +ighlightthickness => '2',-font => 'Arial', -width => '10', -relief => + 'sunken')->pack(-side => 'left'); # ### --- Create Entry Frame my $entry_frame = $MW->Frame(-background => '#DEDE20')->pack(-expand = +> '1', -fill => 'both', -side => 'bottom'); $entry_frame->Label(-background => '#DEDE20', -foreground => 'red', -f +ont => 'Arial', -text => "Enter Chemical Formula:")->pack(-side => 'l +eft'); my $calculate_value = $entry_frame->Entry(-highlightbackground => 'red +',-highlightthickness => '2', -font => 'Arial', -width => '15', -reli +ef => 'sunken')->pack(-side => 'left'); MainLoop; #### BEGIN SECTION FOR SUBROUTINES ---- sub calculate { my $value = $calculate_value->get; my $mass = $mm->calc_mass($value); print "$mass\n"; ## generate "sqwauk box" if invalid entry was made ## resulting in null output if(!(defined $mass)){ my $dialogW = $MW->DialogBox( -title => "ERROR! Please Try Again +.", -buttons => [ "OK" ], ); $dialogW->add("Label", -text => " \"$value\" is not a valid en +try. ", -font => "Arial", -foreground => "red")->pack; ### --- Prevents Dialog Box Resizing $dialogW->bind('<Configure>' => sub{ my $xeW = $dialogW->XEvent; $dialogW->maxsize($xeW->w, $xeW->h); $dialogW->minsize($xeW->w, $xeW->h); }); $dialogW->Show; } $mass_value->delete('0', 'end'); $mass_value->insert('0', $mass); #$name_value->delete('0', 'end'); #$name_value->insert('0', $name); }

Comment on Perl/Tk - Chemistry::MolecularMass
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://281248]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-09-16 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (155 votes), past polls