Tk: Text works with TIE, Scrolled doesn't

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

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

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
s\manfry\Desktop\perl\ line 163
 Tk callback for .frame
 Tk callback for .frame.ysbslice
 Tk callback for .frame.ysbslice.corner
 Carp::croak at C:/perl/lib/ line 269
 Tk::Widget::__ANON__ at C:/perl/site/lib/Tk/ line 347
 main::stampa_struttura at C:\Documents and
Settings\temp\Desktop\perl\ line 163
 Tk callback for .button1
 Tk::__ANON__ at C:/perl/site/lib/ line 252
 Tk::Button::butUp at C:/perl/site/lib/Tk/ line 111
 (command bound to event)

Re: Tk: Text works with TIE, Scrolled doesn't

Quoted text here. Click to load it

Yes, because the Text widget (more accurately, the class Tk::Text)
implements TIE methods.

Quoted text here. Click to load it

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;

@_=map"}split/!/,'/5!*2!+$]!/10+$]';use Tk;$m=tkinit;$t='just an'.
'other perl hacker';$z='createText';$c=$m->Canvas(-wi,$_[1],-he,25)->grid;$c->$

Re: Tk: Text works with TIE, Scrolled doesn't

On Thu, 14 Jul 2005 22:13:24 +0000 (UTC), lusol

Quoted text here. Click to load it

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,

Site Timeline