Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Syntax error using Tk

by eoin (Monk)
on Feb 16, 2004 at 14:54 UTC ( #329309=perlquestion: print w/replies, xml ) Need Help??

eoin has asked for the wisdom of the Perl Monks concerning the following question:

I'm just playing around with Tk and decided to try a calculator out for size.
I think i've got the logic nailed but i'm havin problems doing up the gui with tk.
I get a syntax error with this code, now the method that I have used should work in theory( I think ) but I'm doing something wrong somewhere.
my $mw = new MainWindow; $mw->geometry("300x300"); my $num_screen = $mw->Entry(-width => 40, -state => 'disabled', -textv +ariable => "$ans")->pack(-pady => 30, -anchor => 'ne' -side => 'top') +; my @buttons = ( '9','8','7','6','5','4','3','2','1','0'); for (my $i = 9; $i >= 0; $i--) { ###LINE 17 my $button{$i} = $mw->Button(-text => "$buttons[$i]", -width => '3', - +height => '3', -command => &numpress($i))->pack(-anchor => 'nw', -sid +e => 'top'); }
I get the error
syntax error at line 17, near "$button{"
Execution of aborted due to compilation errors (#1)
I wrote the code above to try and create the number buttons with as least code as possible. If any body has a better way feel free to tell me.
All help is really appriciated.

Cheers, Eoin...

Replies are listed 'Best First'.
Re: Syntax error using Tk
by zentara (Archbishop) on Feb 16, 2004 at 15:19 UTC
    You have a couple of errors, and don't forget to use warnings; and use strict;

    First, you forgot a comma after 'ne' in your first pack.

    Predeclare %button, and get rid of the my before $button{$i}.

    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow; $mw->geometry("300x300"); my %button; my $ans = 'foobar'; my $num_screen = $mw->Entry(-width => 40, -state => 'disabled', -textvariable => "$ans") ->pack(-pady => 30, -anchor => 'ne', -side => 'top'); my @buttons = ( '9','8','7','6','5','4','3','2','1','0'); for (my $i = 9; $i >= 0; $i--) { $button{$i} = $mw->Button( -text => "$buttons[$i]", -width => '3', -height => '3', #-command => &numpress($i)) )->pack(-anchor => 'nw', -side => 'top'); } MainLoop;
    -- I'm not really a human, but I play one on earth. flash japh
      All true. I would also recommend using 'grid' instead of pack to create a better calculator-style layout.

      Also the usage of assigning $buttons[$i] as a Button and overiding the value of the text value $button[$i] forever seems to be a little odd. It will work, but I find it a little odd.

        I've taken your advice and looked into Grid and believe it to be a better choice altogether. I've done a bit of reading and came up with this code but it goes into a continuous loop???
        my $row = 0; my $column = 0; for (my $i = 9; $i >= 0; $i--) { $button{$i} = $mw->Button(-text => "$i", -width => '3', -height => '1', -command => &numpress($i)) ->grid(-row => $row, -column => $column); $column++; if($column > 2){$column = 0; $row++;} } MainLoop;
        Any Ideas???

        Cheers, Eoin...
Re: Syntax error using Tk
by flyingmoose (Priest) on Feb 16, 2004 at 15:10 UTC
    No problem. An array element is indexed like $button[$i] not $button{$i}. Curly braces are for hashes.

    If you think you might be doing a lot of GUI programming, O'Reilly's "Mastering Perl/Tk" is a good reference on the subject that mentions a lot of things you wouldn't think about doing. It covers things like drawing on a Tk Canvas, icons, buttons, menus, and so on... It's a good book.

Re: Syntax error using Tk
by benn (Vicar) on Feb 16, 2004 at 15:54 UTC
    Just a quick addendum to the fine answers given above...

    I think it would have been helpful for you to try to eliminate the Tk aspect, at which point you may have spotted that the problem was (as Perl told you) a basic syntax error, and nothing to do with Tk itself. As you knew that the error was in line 17, cutting and pasting the relevant code portion into another file (or simply commenting out the surrounding bits) would have given you...
    my @buttons = ( '9','8','7','6','5','4','3','2','1','0'); for (my $i = 9; $i >= 0; $i--) { my $button{$i} = $buttons[$i]; # for instance }
    ...which would produce the same syntax error, but would be easier to disect. In addition, playing with just this portion of the code may give you ideas on other ways of producing the results you require. (For instance, it's possible to eliminate the @buttons array completely, should you wish...)

Re: Syntax error using Tk
by converter (Priest) on Feb 16, 2004 at 15:59 UTC

    If you need some help getting started with Perl/Tk, I've placed links to some helpful resources on my home node, including material from one of the co-authors of the aforementioned Tk book.

Re: Syntax error using Tk
by Courage (Parson) on Feb 16, 2004 at 19:36 UTC
    You'll better use variable references in "-textvariable":
    my $var; # .... and then ...., -textvariable => \$var, ...

    Courage, the Cowardly Dog

Re: Syntax error using Tk
by JamesNC (Chaplain) on Feb 17, 2004 at 04:52 UTC
    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...

      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://329309]
Approved by flyingmoose
Front-paged by Courage
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2020-07-05 07:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found