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

In reply to Re: Undefined Button method?? by zentara
in thread Undefined Button method?? by eoin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.