Tk: Text works with TIE, Scrolled doesn't

Discussion in 'Perl Misc' started by Stefan H., Jul 14, 2005.

  1. Stefan H.

    Stefan H. Guest

    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)
    Stefan H., Jul 14, 2005
    #1
    1. Advertising

  2. Stefan H.

    lusol Guest

    Stefan H. <> wrote:
    > 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
    --
    @_=map{eval"100${_}"}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->$
    z(@_[2,3],-te,$t,-fi,'gray50');$c->$z($_[2]-$],$_[3]-$],-te,$t);$m->bind('<En'.
    'ter>',sub{$y=int(rand($m->screenheight));$m->geometry("+$y+$y")});MainLoop;
    lusol, Jul 14, 2005
    #2
    1. Advertising

  3. Stefan H.

    Stefan H. Guest

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

    >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
    Stefan H., Jul 20, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo
    Replies:
    4
    Views:
    465
    clintonG
    May 8, 2004
  2. Simon Andrews
    Replies:
    2
    Views:
    711
    Knute Johnson
    Dec 7, 2006
  3. Replies:
    0
    Views:
    348
  4. botfood

    tie() with DB_File not tie()ing ?

    botfood, Apr 24, 2006, in forum: Perl Misc
    Replies:
    23
    Views:
    441
    botfood
    Apr 26, 2006
  5. botfood
    Replies:
    9
    Views:
    117
Loading...

Share This Page