Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Here's one I prepared earlier :).

I originally posted this RPN calculator to batkins' Tk wiki before it got spamblatted. This formed a tutorial in using the grid geometry manager, which unfortunately seems to have been lost. darned spammers :(.

Reverse Polish Notation syntax is easier for computers to deal with, and is easier to code. The Tk display and buttons would be the same for an algebraic calculator - converting this calculator to algebraic is left as an exercise for the reader.

#!/usr/local/bin/perl use strict; use warnings; use Tk; my $main = MainWindow->new; my $disp = $main->Label(-text => 0) ->grid('-','-','-',-sticky => 'e'); my %button; for (0..9,qw!. + - * / En C!) { $button{$_} = $main->Button( -text => $_, -command => [\&click, $_] ); } $button{C}->grid($button{'/'},$button{'*'},$button{'-'},-sticky=>'nsew +'); $button{7}->grid($button{8},$button{9},$button{'+'},-sticky=>'nsew'); $button{4}->grid($button{5},$button{6},'^',-sticky=>'nsew'); $button{1}->grid($button{2},$button{3},$button{En},-sticky=>'nsew'); $button{0}->grid('-',$button{'.'},'^',-sticky=>'nsew'); $main->gridColumnconfigure($_, -weight => 1) for (0..3); $main->gridRowconfigure($_, -weight => 1) for (0..5); MainLoop; # Main scoped variables used to keep track of calculator status my @stack; my $current; # holds partial number being entered as string sub click { local $_ = shift; # Handle number entry - append to $current if (/\d|\./) { defined($current) ? ($current .= $_) : ($current = $_); # Clear key - first click, clear $current # second click, clear @stack # third click, calculator off } elsif (/C/) { defined($current) ? undef($current) : @stack ? (@stack=()) : $main->destroy; # Any other key, move $current to stack and process } else { { no warnings; # to handle the case when the user has + # pushed '.' more than once push @stack, $current+0 if defined($current) || !@stac +k; } undef $current; /\+/ and (@stack-1) ? push @stack,(pop(@stack) + pop(@stack)) : push @stack, 2*(pop @stack); /-/ and (@stack-1) ? push @stack,-(pop(@stack) - pop (@stack)) : push @stack, -(pop @stack); /\*/ and (@stack-1) ? push @stack,(pop(@stack) * pop (@stack)) : push @stack, (pop @stack)**2; /\// and (@stack-1) ? push @stack,1/(pop(@stack) / pop (@stack +)) : push @stack, 1/(pop @stack); } $disp->configure( -text => defined($current) ? $current : @stack ? $stack[-1] : 0); }

--

Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)


In reply to Re: Tk Screen Refresh by rinceWind
in thread Tk Screen Refresh by Andrew_Levenson

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 contemplating the Monastery: (5)
As of 2024-03-29 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found