Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This another script that I have written with Perl/Tk as an interface to brian d foy's module 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 "early stages". 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.

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::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 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::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 in +valid entries.\nVersion 1.4 -- 08\/19\/2001 Changed all widget fon +ts to Arial.\nVersion 1.4.1 -- 12\/04\/2001 Minor text formatting cha +nges in About Box.\nVersion 1.5 -- 02\/16\/2001 Changed Main Windo +w Title to \"The Elements\".\n*************************************** +*********************************************************\nThis progr +am is freeware and may be modified and\/or redistributed \nunder the +same terms as the GNU public license of PERL. \nNo warranty on this c +ode 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')->pac +k(-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); }

In reply to Perl/Tk-Chemistry::Elements 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 romping around the Monastery: (5)
As of 2024-04-19 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found