in reply to Re: Re: Syntax error using Tk
in thread Syntax error using Tk

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.

Replies are listed 'Best First'.
Re: Re: Re: Re: Syntax error using Tk
by eoin (Monk) on Feb 17, 2004 at 16:14 UTC
    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.

      I would do it like so (ie, not use grid )

      #!/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 = new MainWindow(-title => 'Calc', -bg => 'blue'); $mw->geometry("300x300"); my $topframe = $mw->Frame(-height => '40', -width => '300', -bg => 'bl +ack'); $topframe->pack(-side => 'top', -expand => '0', -fill => 'x', -anchor, + 'nw'); my @rows; push @rows, $mw->Frame()->pack( -expand, 1, -fill, 'both', -side, 'top +', -padx, 3, -pady,3) for (0..3); my $display = $topframe->Entry(-justify => 'right', -state => 'disabled', -textvariable => \$calc) ->pack(-expand => '1', -pady => '20', -padx => '30', -fill => 'x', -side => 'left'); $mw->bind("<KeyRelease>" , sub { &keypress } ); my $r =0; my $w = 0; for my $i ( qw/ 7 8 9 4 5 6 1 2 3 0 . C / ) { $button{$i} = $rows[$r] ->Button(-text => "$i", -width => '3', -height => '1', -command => sub { &btnpress($i) }) ->pack( -expand, 1, -fill, 'both', -side, 'le +ft', -padx, 2); $w++; if( $w == 3){ $r++; $w= 0; } } MainLoop; sub btnpress{ 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); } }


      JamesNC
        Ok I'm nearly there. Still a few problems.
        For some unseen reason I'm getting this error message, now I wasn't getting it earlier before I introduced the function buttons i.e + - \ * etc...
        First of all I created another For loop, and another frame to house the function buttons. Esentially it was identical to the first for loop which created the buttons perfectly. But when I tried this loop I got the error message
        Uncaught exception from user code: Can't call method "Button" on an undefined value at C:\Documents and S +ettings\Eoin\Desktop\calc.pl line 66.
        After that happened I tried to figure out what was wrong but I couldn't spot it, so Idecided to just use the first loop for all the buttons. So I introduced the extra buttons in the array and added in the part to make the + and = buttons bigger. I tested it and got the same error:
        Uncaught exception from user code: Can't call method "Button" on an undefined value at C:\Documents and S +ettings\Eoin\Desktop\calc.pl line 52.
        I just don't understand it??
        Heres the rest of it.
        my $mw = new MainWindow(-title => 'Calc'); $mw->geometry("300x300"); my $topframe = $mw->Frame(-height => '60', -width => '300')->pack(-sid +e => 'top', -expand => '0', -fill => 'x'); my $btmframe = $mw->Frame(-height => '225', -width => '300',)->pack(-s +ide => 'left', -expand => '1', -fill => 'both'); push @rows, $btmframe->Frame()->pack( -expand => 1, -fill => 'both', - +side => 'top') for (0..3); 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 * n* 4 5 6 - \ 1 2 3 + = 0 . C / ) { if($i eq '+' || $i eq '=') { $h = 2; } $button{$i} = $rows[$r]->Button(-text => "$i", -width => '3', -height => "$h", -command => sub { &btnpress($i) }) ->pack(-expand => 1, -fill => 'both', -padx => 2, -pady => 2, -side => 'left', -ipadx => 5, -ipady => 5); $w++; $h = 1; if($w > 2){$w = 0; $r++;} } MainLoop;

        Cheers, Eoin...