Play with this:
#!/perl/bin/perl -w use strict; use Tk; use Tk::Button; use Tk::Entry; my $row = 0; my $column = 0; my $calc = "0.00"; my $buffer; my %button; my $mw = tkinit(-title, "Calc"); my $display = $mw->Entry(-width, 20, -justify, 'right', -textvariable, \$calc ) ->grid(-column, $column++, -row, $row++, -columnspan, 3, -pady, 2); $column = 0; #how to Capture Key Strokes $mw->bind("<KeyRelease>" , sub { &keypress } ); #use qw to arrange your key layout for my $i ( qw / 7 8 9 6 5 4 3 2 1 0 . C / ) { $button{$i} = $mw->Button(-text => "$i", -width => '5', -height => '1', -command => sub { &numpress($i)} ) ->grid(-row => $row, -column => $column, -padx, 2, -pady, 2); $column++; if($column > 2){$column = 0; $row++;} } MainLoop; sub numpress{ my $num = shift; if($num ne "C"){ $buffer .= $num; $calc = sprintf "%0.2f", $buffer; } if($num eq "C"){ $calc = "0.00"; $buffer = 0.00; } } sub keypress{ my $widget = shift; my $e = $widget->XEvent; # get event object my $key = $e->K; $key=~s/period/\./ig; if( $key =~/c/ig ){ $key = uc $key; $button{$key}->invoke; } if( $key =~/(\d)|\./){ &numpress($key); } }

In addition to the other suggestions, here are a few other ideas you might play with... this will show you how to capture keystrokes, invoke the buttons from other subs and load your buttons in different method...

JamesNC

In reply to Re: Syntax error using Tk by JamesNC
in thread Syntax error using Tk by eoin

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.