Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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); }

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found