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

Hi, I have this error when I select different month in the drop down box then hit "print value" any body know why or how to fix the error? There are no error if I don't click on the drop down.

Thanks in advance.

Tk::Error: Can't call method "butUp" on an undefined value at C:/Perl/site/lib/T k/BrowseEntry.pm line 375. <ButtonRelease-1> (command bound to event) Code
# BrowseEntry, entry with listbox to select list values. use strict; use warnings; use Tk; use Tk::BrowseEntry; my $month = "January"; my $top = MainWindow->new; my $f = $top->Frame->pack; my $top_f = $f->Frame(-bg=>'red', -width=>'200')->pack; my $bot_f = $f->Frame(-bg=>'red', -width=>'200')->pack; my $c = $top_f->BrowseEntry(-label => "Month:", -variable => \$mon +th); $c->pack; $c->insert("end", "January"); $c->insert("end", "February"); $c->insert("end", "March"); $c->insert("end", "April"); $c->insert("end", "May"); $c->insert("end", "June"); $c->insert("end", "July"); $c->insert("end", "August"); $c->insert("end", "September"); $c->insert("end", "October"); $c->insert("end", "November"); $c->insert("end", "December"); $bot_f->Button(-text => "Print value", -command => sub { print "The month is $month\n"; $top_f->destroy if Tk::Exists($top_f); }, -relief => "raised")->pack; MainLoop;

Replies are listed 'Best First'.
Re: Perl TK BrowseEntry error
by stefbv (Priest) on Jan 10, 2010 at 17:20 UTC

    Do you really need to destroy the 'top_f' frame? If you comment the line

    $top_f->destroy if Tk::Exists($top_f);
    then the error will vanish.

    I presume is a normal error for the way you use destroy, but I don't know enough about the internals of Tk to give you a technical explanation.

Re: Perl TK BrowseEntry error
by biohisham (Priest) on Jan 10, 2010 at 17:58 UTC
    I can't explain the error that you're getting effectively, it seems that there's a widget event associated with the mouse left button release click. Here is another way to do it...by using a "scrolled Listbox" and by double clicking on a month you can get its corresponding value printed to the console, also you can do the same from clicking the button as you've originally intended:
    #!/usr/local/bin/perl use strict; use warnings; use Tk; my $mainWindow = MainWindow->new(-title=>"Main Window"); my $listBox = $mainWindow->Scrolled( 'Listbox', -scrollbars=>'e' )->pack; $listBox->insert('end',qw(Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov + Dec)); $listBox->bind('<Double-1>', \&getActive); $mainWindow->Button( -text=>'print value', -command=>\&getActive, )->pack(); sub getActive{ my $month; $month = $listBox->get('active'); #detects a selected list item print $month,"\n"; }; MainLoop;

    Update: On closer inspection, why do you want to destroy the frame "$top_f" since it contains the widget that lets you browse the months collection?

    my $button=$top->Button( -text=>'Print Value', -command => \&printMonth, #-relief=>'raised' )->pack(); sub printMonth{ print "The Current Month is $month\n"; }


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: Perl TK BrowseEntry error
by ldln (Pilgrim) on Jan 10, 2010 at 18:54 UTC
    Think this behavior is caused by small bug in BrowseEntry.

    Try replacing:

    $top_f->destroy if Tk::Exists($top_f); with $top_f->packForget( ) if $top_f->ismapped;
Re: Perl TK BrowseEntry error
by fast (Novice) on Jan 10, 2010 at 19:46 UTC
    Thank you all for reply. This is just an example code. The reason I destroy the top_f is because I want to replace with a different screen after the user finish selecting. I think "ldln" is right about the bug in BrowseEntry widge.
    Is packForget similar to destroy?.
    $top_f->packForget( ) if $top_f->ismapped;
      Hi,

      packForget will hide the widget. You can call pack on it to display it again. The instance will stay in memory. Unless you need $top_f again, you could use afterIdle to delay destroying the frame until the Buttons -command-callback has returned:

      $bot_f->Button(-text => "Print value", -command => sub { print "The month is $month\n"; $top->afterIdle(sub{ $top_f->destroy if Tk::Exists($top_f); }); }, -relief => "raised")->pack;

      Cheers, Christoph