CUFP
PERLscienceman
This another script that I have written with Perl/Tk
as an interface to brian d foy's module [cpan://Chemistry::Elements]
Being of an Earth Sciences background I saw this module,
thought it was cool, decided to put it to Tk, and see what happened.
I still consider this code to be in its <i>"early stages"</i>.
For right now, you enter the element symbol, click on Find,
and if it is a valid element symbol you get the element name
and atomic number. It has been a while since I have worked
on this one. The goal for the next version is to be able
to enter a value into any of the boxes and get appropriate output in the other two.
<p>
As always, any input or suggestions for improvement of
this code are greatly appreciated. Enjoy and Have Fun!<p>
Learning Is What Makes Life Interesting!<p>
<hr noshade>
<code>
#!/usr/bin/perl -w
#
use English;
use Chemistry::Elements qw(get_name get_Z get_symbol);
use Tk;
use Tk::DialogBox;
use strict;
sub convert ;
my $MW = MainWindow->new;
### --- 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("The Elements");
### --- Create Button Frame
my $button_frame = $MW->Frame();
$button_frame->pack(-side => 'bottom', -expand => '1', -fill => 'both');
### --- Create About Button
$button_frame->Button(-text => "About",
-foreground => "#007300",
-activebackground => "#98FB98",
-font => 'Arial',
-command => sub
{
my $dialogA = $MW->DialogBox( -title => "About this program",
-buttons => [ "OK" ],
);
$dialogA->add("Label",
-borderwidth => '3',
-background => "#FAEBD7", #ANTIQUE WHITE
-font => 'bold',
-justify => 'left',
-relief => 'sunken', #One seriously long text string follows 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 module Chemistry::Elements.\nVersion 1.0 -- 03\/06\/2001 Original Code Written by Jay Madigan.\nVersion 1.1 -- 03\/09\/2001 Cleaned up \& Commented code, implemented use strict;.\nVersion 1.2 -- 03\/10\/2001 Added code to prevent Main Window & Dialog Box resizing\r\t - resized (enlarged) About dialog box font for better readability.\nVersion 1.3 -- 05\/26\/2001 Added Error Message Dialog Box for invalid entries.\nVersion 1.4 -- 08\/19\/2001 Changed all widget fonts to Arial.\nVersion 1.4.1 -- 12\/04\/2001 Minor text formatting changes in About Box.\nVersion 1.5 -- 02\/16\/2001 Changed Main Window Title to \"The Elements\".\n************************************************************************************************\nThis program 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",
-font => 'Arial',
-command => sub
{
exit;
});
$exit->pack(-side => 'right', -expand => '1', -fill => 'both');
### --- Create Convert Button
my $convert = $button_frame->Button(-text => 'Find',
-foreground => "blue",
-activebackground => "#ADD8E6",
-font => 'Arial',
-command => sub
{
convert;
});
$convert->pack(-side => 'right', -expand => '1', -fill => 'both');
### --- Create Element Atomic Number Frame
my $number_frame = $MW->Frame()->pack(-expand => '1', -fill => 'both', -side
=> 'bottom');
$number_frame->Label(-text => "Atomic number:", -font => 'Arial')->pack(-side => 'left');
my $number_value = $number_frame->Entry(-font => 'Arial', -width => '3', -relief => 'sunken')->pack(-side => 'left');
#
#
### --- Create Element Name Frame
my $name_frame = $MW->Frame()->pack(-expand => '1', -fill => 'both', -side
=> 'bottom');
$name_frame->Label(-text => "Element name:", -font => 'Arial')->pack(-side => 'left');
my $name_value = $name_frame->Entry(-font => 'Arial', -width => '12', -relief => 'sunken')->pack(-side => 'right');
#
#
### --- Create Entry Frame
my $entry_frame = $MW->Frame()->pack(-expand => '1', -fill => 'both', -side =>
'bottom');
$entry_frame->Label(-foreground => 'red', -font => 'Arial', -text => "Enter element symbol:")->pack(-side => 'left');
my $convert_value = $entry_frame->Entry(-highlightbackground => 'red',-highlightthickness => '1', -font => 'Arial', -width => '3', -relief => 'sunken')->pack(-side => 'left');
MainLoop;
#### BEGIN SECTION FOR SUBROUTINES ----
sub convert {
my $question = $convert_value->get;
my $name = get_name($question);
my $number = get_Z($question);
print "$name\n";
## generate "sqwauk box" if invalid entry was made
## resulting in null output
if(!(defined $name)){
my $dialogW = $MW->DialogBox( -title => "ERROR! Please Try Again.",
-buttons => [ "OK" ],
);
$dialogW->add("Label", -text => " \"$question\" is not a valid entry. ",
-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;
}
$number_value->delete('0', 'end');
$number_value->insert('0', $number);
$name_value->delete('0', 'end');
$name_value->insert('0', $name);
}
</code>