in reply to Undefined Button method??

First, always post full code, otherwise we HAVE TO GUESS at what you are doing wrong. In the code you posted, half of your variables are uninitialized, so I had to improvise. Second, I thought you were going to switch to grid, which is better suited for this. So that said, I've modified your code to work, but without grid, all your buttons end up in one row. Either switch to grid, or make a frame for each row, and when you make your buttons, switch to the next frame when your $counter goes over 4 or whatever. I think your original problem came from the way you setup your
qw/ 7 8 9 * n* 4 5 6 - \ 1 2 3 + = 0 . C / ) {
and it was getting an undef value in there.

I'm not going to do your project for you. The code below is a patched up version of your code, which runs. Getting the rows setup right is up to you. Switch to grid, or setup separate frames for your rows of buttons.

#!/usr/bin/perl use Tk; use warnings; use strict; my %button; my $calc = 0; my $mw = new MainWindow( -title => 'Calc' ); $mw->geometry("300x300"); my $topframe = $mw->Frame( -height => '60', -width => '300' ) ->pack( -side => 'top', -expand => '0', -fill => 'x' ); my $btmframe = $mw->Frame( -height => '225', -width => '300', ) ->pack( -side => 'left', -expand => '1', -fill => 'both' ); push my @rows, $btmframe->Frame()->pack( -expand => 1, -fill => 'both', -side => 't +op' ) 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 } ); my $r = 1; my $h = 1; my $w = 1; for my $i (qw/ 7 8 9 * n* 4 5 6 - \ 1 2 3 + = 0 . C / ) { if ( $i eq '+' || $i eq '=' ) { $h = 2; } ########LINE 52 BELOW################ $button{$i} = $mw->Button( -text => "$i", -width => '1', -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;

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re: Re: Undefined Button method??
by eoin (Monk) on Feb 19, 2004 at 17:23 UTC
    I tried out grid and it worked for the most part but I wanted to get the buttons to resize if I resized the window. I couldn't figure out how to do this with grid. JamesNC suggested a way of using pack to get the buttons right, the seperate frames, just as you said. And that worked nicely.
    Thanks for your advice any way. If you know how to get the buttons to expand I'd be more than happy to give grid a try again.

    Cheers, Eoin...