For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > May 2004 > fileevent problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author fileevent problem
bxb7668

2004-05-04, 1:59 pm

I have a script that uses Tk to gather inputs and then recursively
calls itself to display some other processing using fileevent. When it
runs I get this:

Tk::Error: Can't call method "fileevent" on an undefined value at
tkboom.pl line 82.
Tk callback for .frame1.button
Tk::__ANON__ at /ots/perl-5.6.1/lib/site_perl/5.6.1/aix/Tk.pm line
228
Tk::Button::butUp at
/ots/perl-5.6.1/lib/site_perl/5.6.1/aix/Tk/Button.pm line 111
(command bound to event)

I'm new to Tk. What am I doing wrong?
P.S. Does anyone have a Notepad like file browser widget that I can
reuse? I'd be very thankful.

Brian

Here is a stripped down version of the script that does display this
message:

#!/usr/bin/perl -w
use strict;
use Tk;

my $WINDOWS = 1;
my $NUMPARMS = @ARGV;
$WINDOWS = 0 if ( $NUMPARMS > 0 );
my $CMD = $0;

# Declare global variables
my $BUTTON;
my $MAINWIN;
my $hSCROLLED;

# If input prompting
if ( $WINDOWS ) {
&Tk_GUI(); # Launch the graphical interface
} else {
# Process the output to the command line or fileevent
&process( );
}
exit 0;

# ========================================
=================
# Subroutines
# ========================================
=================
sub process {
# Do some print statements
print "
========================================
===================
|| tkboom: (version 4.0 04/30/04)
========================================
===================
";
print "NOTE: This command may be rather slow \n";
print "\nOutput Listing is Complete\n";
}
# ========================================
=================
sub Tk_GUI {
# The purpose of this subroutine is to dispaly the GUI for this tool
my $MAINWIN = MainWindow->new;
# -----------------------------------------------------
# Put widgets in the Tool processing text scroll field
# -----------------------------------------------------
my $fScroll = $MAINWIN->Frame()->pack(-side => 'top', -fill => 'x');

# Create the fileevent widget
$hSCROLLED = $fScroll->Scrolled("Text", -width => 100,
-height => 25, -wrap => 'none')->pack( -expand =>
1 );
# -----------------------------------------------------
# Put widgets in the bottom button frame
# -----------------------------------------------------
my $fBBtn = $MAINWIN->Frame()->pack(-side => 'top', -fill => 'x');

$BUTTON = $fBBtn->Button( -text => "Continue",
-command => [\&continue])->pack(-side => 'bottom');

MainLoop;

} # End Tk_GUI subroutine
# ========================================
=================
sub continue {

# When the Continue button is pressed, validate the inputs and
# process the information and change the button to Quit

$BUTTON->configure(-text => "Cancel Processing", -command => [
\&exit ] );

# Execute findall
open(CHILD, "$CMD recurse 2>&1 |") or die "Cannot open $!";
print "DEBUG: CMD: $CMD\n";
$MAINWIN->fileevent(\*CHILD, 'readable', [\&fill_text_widget,
$hSCROLLED ]);

$BUTTON->configure(-text => "Quit", -command => [ \&exit ] );
}
# ========================================
=================
sub fill_text_widget {
my ($widget) = @_;
$_ = <CHILD>;
$widget->insert('end', $_ );
$widget->yview('end');
}


Ala Qumsieh

2004-05-04, 11:36 pm

"bxb7668" <bxb7668@somewhere.nocom> wrote in message
news:Hx04Gz.Hv1@news.boeing.com...
> I have a script that uses Tk to gather inputs and then recursively
> calls itself to display some other processing using fileevent. When it
> runs I get this:
>
> Tk::Error: Can't call method "fileevent" on an undefined value at
> tkboom.pl line 82.
> Tk callback for .frame1.button
> Tk::__ANON__ at /ots/perl-5.6.1/lib/site_perl/5.6.1/aix/Tk.pm line
> 228
> Tk::Button::butUp at
> /ots/perl-5.6.1/lib/site_perl/5.6.1/aix/Tk/Button.pm line 111
> (command bound to event)
>
> I'm new to Tk. What am I doing wrong?
> P.S. Does anyone have a Notepad like file browser widget that I can
> reuse? I'd be very thankful.


I'm not sure what you mean by this. Are you looking for an editor? Try
nedit, or xemacs.
Btw, your problem has nothing to do with Tk. It's a variable scoping issue.
Another thing. Don't call your subs as '&sub()'. The ampersand isn't needed,
unless you know why it has to go there. Simply use 'sub()' instead.

> # ========================================
=================
> sub Tk_GUI {
> # The purpose of this subroutine is to dispaly the GUI for this tool
> my $MAINWIN = MainWindow->new;


my() declares a lexical variable that will only be seen in the Tk_GUI() sub.
This variable's scope is strictly inside the Tk_GUI() sub and has NOTHING to
do with your other lexical $MAINWIN variable that you defined at the
beginning of your program. The scope of the other $MAINWIN variable is the
whole file that contains the script and thus can be seen by other
subroutines.

So, to fix your problem, remove the 'my' on this line.

I suggest you read MJD's 'Coping with Scoping' article for further
enlightenment:

http://www.foo.be/docs/tpj/issues/v...j0304-0001.html

--Ala


bxb7668

2004-05-05, 12:41 am

Thank you Ala. That was the problem. I do understand lexical scope. I
just missed the extra "my" when I moved the Tk into the subroutine.

What I need the notepad-like widget for is to display a file created
by a script to the user and give them a chance to save it to another
name. I can build it, but if someone already has something that works,
I'm not opposed to reuse. My project is already a couple months behind
schedule.

Brian

"Ala Qumsieh" <xxala_qumsiehxx@xxyahooxx.com> wrote in message
news:aszkc.43239$nN2.19292@newssvr29.news.prodigy.com...
> "bxb7668" <bxb7668@somewhere.nocom> wrote in message
> news:Hx04Gz.Hv1@news.boeing.com...
When it[color=darkred]
line[color=darkred]
can[color=darkred]
>
> I'm not sure what you mean by this. Are you looking for an editor?

Try
> nedit, or xemacs.
> Btw, your problem has nothing to do with Tk. It's a variable scoping

issue.
> Another thing. Don't call your subs as '&sub()'. The ampersand isn't

needed,
> unless you know why it has to go there. Simply use 'sub()' instead.
>
tool[color=darkred]
>
> my() declares a lexical variable that will only be seen in the

Tk_GUI() sub.
> This variable's scope is strictly inside the Tk_GUI() sub and has

NOTHING to
> do with your other lexical $MAINWIN variable that you defined at the
> beginning of your program. The scope of the other $MAINWIN variable

is the
> whole file that contains the script and thus can be seen by other
> subroutines.
>
> So, to fix your problem, remove the 'my' on this line.
>
> I suggest you read MJD's 'Coping with Scoping' article for further
> enlightenment:
>
> http://www.foo.be/docs/tpj/issues/v...j0304-0001.html
>
> --Ala
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com