in reply to Re: TK::ExecuteCommand
in thread TK::ExecuteCommand

here is the code, it is from the book mastering perl/tk.
also i chaned the command, cause this one here is for unix.

$Tk::ExecuteCommand::VERSION = '1.1'; package Tk::ExecuteCommand; use IO::Handle; use Proc::Killfam; #use Tk::widgets qw/LabEntry ROText/; use Tk::widgets qw/ROText/; use base qw/Tk::Frame/; use strict; Construct Tk::Widget 'ExecuteCommand'; sub Populate { my($self, $args) = @_; $self->SUPER::Populate($args); my $f1 = $self->Frame->pack; $f1->LabEntry( -label => 'Command to Execute', -labelPack => [qw/-side left/], -textvariable => \$self->{-command}, )->pack(qw/-side left/); my $doit = $f1->Button(-text => 'Do It!')->pack(qw/-side left/); $self->Advertise('doit' => $doit); $self->_reset_doit_button; $self->Frame->pack(qw/pady 10/); $self->Label(-text => 'Command\'s stdout and stderr')->pack; my $text = $self->Scrolled('ROText', -wrap => 'none'); $text->pack(qw/-expand 1 -fill both/); $self->Advertise('text' => $text); $self->OnDestroy([$self => 'kill_command']); $self->{-finish} = 0; $self->ConfigSpecs( -command => [qw/METHOD command Command/, 'sleep 5; pwd'], ); } # end Populate sub command { my($self, $command) = @_; $self->{-command} = $command; } # end command sub _flash_doit { # Flash "Do It" by alternating its background color. my($self, $option, $val1, $val2, $interval) = @_; if ($self->{-finish} == 0) { $self->Subwidget('doit')->configure($option => $val1); $self->idletasks; $self->after($interval, [\&_flash_doit, $self, $option, $val2, $val1, $interval]); } } # end _flash_doit sub _read_stdout { # Called when input is available for the output window. Also chec +ks # to see if the user has clicked Cancel. my($self) = @_; if ($self->{-finish}) { $self->kill_command; } else { my $h = $self->{-handle}; if ( sysread $h, $_, 4096 ) { my $t = $self->Subwidget('text'); $t->insert('end', $_); $t->yview('end'); } else { $self->{-finish} = 1; } } } # end _read_stdout sub _reset_doit_button { # Establish normal "Do It" button parameters. my($self) = @_; my $doit = $self->Subwidget('doit'); my $doit_bg = ($doit->configure(-background))[3]; $doit->configure( -text => 'Do It', -relief => 'raised', -background => $doit_bg, -state => 'normal', -command => [sub { my($self) = @_; $self->{-finish} = 0; $self->Subwidget('doit')->configure( -text => 'Working ...', -relief => 'sunken', -state => 'disabled' ); $self->execute_command; }, $self], ); } # end _reset_doit_button # Public methods. sub execute_command { # Execute the command and capture stdout/stderr. my($self) = @_; my $h = IO::Handle->new; die "IO::Handle->new failed." unless defined $h; $self->{-handle} = $h; $self->{-pid} = open $h, $self->{-command} . ' 2>&1 |'; if (not defined $self->{-pid}) { $self->Subwidget('text')->insert('end', "'" . $self->{-command} . "' : $!\n"); $self->kill_command; return; } $h->autoflush(1); $self->fileevent($h, 'readable' => [\&_read_stdout, $self]); my $doit = $self->Subwidget('doit'); $doit->configure( -text => 'Cancel', -relief => 'raised', -state => 'normal', -command => [\&kill_command, $self], ); my $doit_bg = ($doit->configure(-background))[3]; $self->_flash_doit(-background => $doit_bg, qw/cyan 500/); } # end execute_command sub kill_command { # A click on the blinking Cancel button resumes normal operations. my($self) = @_; $self->{-finish} = 1; my $h = $self->{-handle}; return unless defined $h; $self->fileevent($h, 'readable' => ''); # clear handler killfam 'TERM', $self->{-pid} if defined $self->{-pid}; close $h; $self->_reset_doit_button; } # end kill_command 1;

Edit by BazB: remove pre tags around text and add readmore tags around code.

Replies are listed 'Best First'.
Re^3: TK::ExecuteCommand
by davidj (Priest) on Jun 24, 2004 at 12:26 UTC
    If this is the code you tried to run as D:\...\exec.pl, then that is your problem. This code is the module definition for Tk::ExecuteCommand. It cannot be executed by itself. Forget about trying to run it. If you are on windows, use ppm to install the module from one of the repositories and test it with the example code I have already posted.

    davidj
      thanks problem solved
      $ec->terse_gui;
      has to be called to disable the gui.