ariels has asked for the wisdom of the Perl Monks concerning the following question:

I'm using Tk. Is it possible to make 2 Tk::Text widgets share their contents? I can do this for 2 Tk::Entry widgets (by pointing their `-textvariable' option at the same variable). Must I add my own code to copy text changes between the 2 widgets?

Replies are listed 'Best First'.
Re: Sharing Tk::Text?
by graff (Chancellor) on May 14, 2002 at 03:06 UTC
    Sorry if this late response serves no purpose for you. (I was trolling in super-search for discussions about binding the tab character in Tk::Text... still looking... but found your node and had a suggestion.)

    Yes, you do need to provide your own code to copy text changes between two Text widgets. And probably the nicest way to do this (from the user's perspective) would be to bind your own "copyText" function to a suitable event for the Text widget class. The best event to use probably depends on the app, but my first thought would be something like:

    $mainwindow->bind( 'Tk::Text', '<Leave>', [\&copyText, $tw1, $tw2] ) +;
    where $tw1 and $tw2 are the widget references for the two Text windows that you want to keep in sync.

    This binding says that whenever the mouse cursor moves out of any Text widget, "copyText()" will be called, receiving that particular widget reference as it's first arg, and the two text widget references as second and third args. The sub then goes something like this (not tested):

    sub copyText { my ($trigger,$tw1,$tw2) = @_; my ($src,$dst,$txt); if ($trigger eq $tw1) { $src = $tw1; $dst = $tw2; } else { $src = $tw2; $dst = $tw1; } $txt = $src->get('1.0','end'); $dst->delete('1.0','end'); $dst->insert('1.0',$txt); }
    Of course, if the Text contents are complicated (tags, images, etc -- anything other than simple, plain text), then this sub needs to be a little more complicated....
Re: Sharing Tk::Text?
by Eradicatore (Monk) on Jun 14, 2001 at 16:26 UTC
    I'm sure that this is not possible. That is to say, this is not possible with the standard distribution. This seems to be a perfect opportunity to "give something back" to your fellow perl/tk community members, by editing the Tk::Text module yourself.

    Just a thought. I'm not sure which would be easier. Doing that, or doing what you said and adding code around Tk::Text to make two of these beasts share nice. Since it doesn't seem to "interesting" to have two identical text widgets, I'm thinking you would want to probably add your code around this anyway, because do you really want them exactly the same??

    Anyway, good luck. I'm a big fan of perl/tk so let me know if you have other questions.

    Justin Eltoft

    "If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews