john.tm has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl tk gui with a Help_Guide command button and a text widget, which is filled from a txt file, i am trying to add a close_help button that will clear the contents of the text widget but get this error
Tk::Error: Not a CODE reference at C:/Perl/site/lib/Tk.pm line 251. Tk callback for .frame.frame.button3 Tk::__ANON__ at C:/Perl/site/lib/Tk.pm line 251 Tk::Button::butUp at C:/Perl/site/lib/Tk/Button.pm line 175 <ButtonRelease-1> (command bound to event)
This my updated code which unfortunately still does not clear the text loaded from the help button.
use warnings; use strict; use Tk; use Tk::Text ; use POSIX 'strftime'; my $DATE = strftime("Run Failures.pl for %dth %b %Y" , localtime()); my $mw = MainWindow->new; my $filename = "c:\\Temp\\perl.txt"; $mw->geometry("720x500"); $mw->title(" backupadmin "); my $main_frame = $mw->Frame()->pack(-side => 'top', -fill => 'x'); my $left_frame = $main_frame->Frame(-background => "snow2")->pack(-sid +e => 'left', -fill => 'y'); my $right_frame = $main_frame->Scrolled("Text", -scrollbars => 'se',-b +ackground => "black",-foreground => "yellow",-height => '44')->pack(- +expand => 1, -fill => 'both'); my $failures_button = $left_frame->Button(-text => " $DATE ", -command => \&runscript)->pack; my $Close_button = $left_frame->Button(-text => ' + Close ', -command => $mw => 'destroy')->pack; my $Help_button = $left_frame->Button(-text => " Help + Guide ", -command => \&load_file)->pack(-side = +> "bottom"); my $Close_help = $left_frame->Button(-text => ' Close Help ', -command => \&clear_file )->pack(-side => "bottom"); MainLoop; sub runscript { system("daily_failures.pl"); # or if it takes some time to run the script #system("daily_failures.pl &"); # runs in the background } sub load_file { open (FH, "$filename"); while (<FH>) { $right_frame->insert("end", $_); } close (FH); } sub clear_file { $right_frame->delete('1.0','end'); $right_frame->packForget; }

Replies are listed 'Best First'.
Re: perl tk how to clear contents of text widget
by zentara (Cardinal) on Jun 28, 2014 at 17:40 UTC
    The command to delete all text in a Tk::Text widget is
    $text->delete("1.0", 'end');
    your command
    -command => [$right_frame => \&clear_file])->pack(-side => "bottom")
    makes no sense. A Scrolled Text widget dosn't know what => means. Try this
    my $Close_help = $left_frame->Button(-text => ' Close Help ', -command => \&clear_file )->pack(-side => "bottom"); sub clear_file { $right_frame->delete('1.0','end'); $right_frame->packForget; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      This still does not clear the text from the text widget.
        It still dosn't clear

        You seem to like to use the array form for the -command. The code I showed you does NOT have the brackets.

        -command => [$right_frame => \&clear_file]) #notice the difference -command => \&clear_file )
        Don't use the bracketed command form unless you know what you are doing with it. It is used if you want to pass arguments to a subroutine, which you don't need in this case.

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
        Can you prove it? Can you copy/paste your new program?
Re: perl tk how to clear contents of text widget
by Anonymous Monk on Jun 28, 2014 at 19:29 UTC

    ... Tk::Error: Not a CODE reference ...

    You wrote  -command => [$right_frame => \&clear_file] but $right_frame is not \&clear_file, you switched the order for some reason