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); }

In reply to Perl/Tk - Chemistry::MolecularMass by PERLscienceman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.