in reply to Syntax error using Tk

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

Replies are listed 'Best First'.
Re: Re: Syntax error using Tk
by eoin (Monk) on Feb 17, 2004 at 10:25 UTC
    Thanks this really helped. You see I was unaware that you couldn;t mix and match geometry managers. I had used pack for my display widget, and then grid for my buttons. This apparently was causing my script to hang.
    Also cheers for pointing out about binding keystrokes, I had clean forgoten about that.

    Nollaig Shona, Eoin...
      You can mix geometry managers using frames. A common approach is to use a grid manager on some widgets inside of a frame. And to pack that frame along with others. Once you get the hang of using frames, it be easy to design any layout.
        Thanks for the advice. I looked into Frames and this is what I came up with. Although I am still having a small problem:
        my $mw = new MainWindow(-title => 'Calc', -bg => 'blue'); $mw->geometry("300x300"); my $topframe = $mw->Frame(-height => '60', -width => '300', -bg => 'bl +ack'); my $btmframe = $mw->Frame(-height => '225', -width => '300', -bg => 'r +ed'); my $display = $topframe->Entry(-justify => 'right', -state => 'disabled', -textvariable => \$calc) ->pack(-expand => '1', -fill => 'x', -pady => 30, -padx => 20, -side => 'right'); $mw->bind("<KeyRelease>" , sub { &keypress } ); for my $i ( qw/ 7 8 9 4 5 6 1 2 3 0 . C / ) { $button{$i} = $btmframe->Button(-text => "$i", -width => '3', -height => '1', -command => sub { &btnpress($i) }) ->grid(-row => $row, -column => $column, -padx => 2, -pady => 2, -sticky => 'nsew', -ipadx => 10, -ipady => 10); $column++; if($column > 2){$column = 0; $row++;} } $topframe->pack(-side => 'top', -expand => '0', -fill => 'x'); $btmframe->pack(-side => 'top', -expand => '1', -fill => 'both'); MainLoop;
        I want to make it so the buttons will expand to fill the frame, so when thw window is resized the buttons will resize accordingly. Is this possible??
        What do you think?
        The frames are coloured just for testing purposes.

        Nollaig Shona, Eoin...

        Nollaig shona duit.