in reply to Syntax error using Tk

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

Replies are listed 'Best First'.
Re: Re: Syntax error using Tk
by flyingmoose (Priest) on Feb 16, 2004 at 15:43 UTC
    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...
        Now the problem is here:
        -command => &numpress($i))
        The thing being assigned to the "-command" attribute needs to be an anonymous subroutine, a reference to a named subroutine, or else a reference to an array whose elements are: named_subroutine_ref, arg1(, arg2 ...) -- in other words, either of the following would be the right way to do what you want:
        -command => sub { numpress( $i ) } # or -command => [ \&numpress, $i ]
        The way you had it written, your subroutine is actually being called when the Button is being created, and Perl/Tk is trying to use the return value of the sub as the value for "-command" -- not good.