Tk: Text works with TIE, Scrolled doesn't

S

Stefan H.

the following code works properly, printing on Text all I print to
STDOUT

my $text = $mw->Text->pack();
tie *STDOUT, ref $text, $text;
print "prova";

but I want scrollbars, so I change to

my $text = $mw->Scrolled("Text")->pack();

at running time Perl output the following error. What's wrong??

Tk::Error: Failed to AUTOLOAD 'Tk::Frame::TIEHANDLE' at C:\Documents and
Setting
s\manfry\Desktop\perl\prova.pl line 163
Tk callback for .frame
Tk callback for .frame.ysbslice
Tk callback for .frame.ysbslice.corner
Carp::croak at C:/perl/lib/Carp.pm line 269
Tk::Widget::__ANON__ at C:/perl/site/lib/Tk/Widget.pm line 347
main::stampa_struttura at C:\Documents and
Settings\temp\Desktop\perl\prova.pl line 163
Tk callback for .button1
Tk::__ANON__ at C:/perl/site/lib/Tk.pm line 252
Tk::Button::butUp at C:/perl/site/lib/Tk/Button.pm line 111
<ButtonRelease-1>
(command bound to event)
 
L

lusol

Stefan H. said:
the following code works properly, printing on Text all I print to
STDOUT

my $text = $mw->Text->pack();
tie *STDOUT, ref $text, $text;
print "prova";

Yes, because the Text widget (more accurately, the class Tk::Text)
implements TIE methods.
but I want scrollbars, so I change to

my $text = $mw->Scrolled("Text")->pack();

at running time Perl output the following error. What's wrong??

Tk::Error: Failed to AUTOLOAD 'Tk::Frame::TIEHANDLE' at C:\Documents and
Setting
s\manfry\Desktop\perl\prova.pl line 163

The Tk Scrolled() method does its magic by creating an outer Frame
widget and placing the Text widget and Scrollbar widgets inside the
Frame - and Scrolled() returns the Tk::Frame reference, not the
Tk::Text reference. Thus, the "tie *STDOUT, ref $text" statement TIEs
to the wrong class. And since Tk::Frame provides no TIE methods, you
see your error.

The trick is to use the Subwidget() method to fetch the Tk::Text
widget reference and tie() to that:

my $frog = $text->Subwidget( 'scrolled' );
tie *STDOUT, ref $frog, $text;

Steve
 
S

Stefan H.

The trick is to use the Subwidget() method to fetch the Tk::Text
widget reference and tie() to that:

my $frog = $text->Subwidget( 'scrolled' );
tie *STDOUT, ref $frog, $text;

thanks Steve for the lesson. I'm a little bit frustrated because I don't
know what I'm doing but it works :)

I have experience on Perl but I never saw things like tie, * and ref.

Thanks again,
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top