How can I create 2 Perl / TK text areas with scrollbars that scroll at the same time when scrolling the same area of ​​text?

Is it possible to use TK to create text areas with scroll bars that move when one of them is scrolled?

What I want to create is a text area with headings and then text areas under the row headings in one and data in the other. Like view when you freeze panels in Excel. I have data in a set of arrays for each row, so all I need is a way to link the scrollbars in each of the textareas, so up one in the data also controls the row headers and vice versa, and the left-right one of the data controls column headings and vice versa.

Perhaps impossible, but it doesn't hurt to ask

EDIT

Ok, so I have some code and it almost does what I want, but I need help to get it to work completely. The code example shows that if you move one scrollbar it does indeed control the other textarea and vice versa, but it does not control its own textarea, is there a way to add multiple xviews to the command so that it moves both textareas to the same time. thanks in advance

use Tk;
use Tk::ROText;

my @headers = ( "+----------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+",
                "|                |  M  |  M  |  M  |  M  |  M  |  M  |  M  |  M  |  M  |  M  |  M  |  M  |  M  |  M  |",
                "|                |  P  |  P  |  P  |  P  |  P  |  P  |  P  |  P  |  P  |  P  |  P  |  P  |  P  |  P  |",
                "|                |  L  |  L  |  L  |  L  |  L  |  L  |  L  |  L  |  L  |  L  |  L  |  L  |  L  |  L  |",
                "|                |  R  |  R  |  R  |  R  |  R  |  R  |  R  |  R  |  R  |  R  |  R  |  R  |  R  |  R  |",
                "|                |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |",
                "|                |  F  |  F  |  F  |  F  |  F  |  F  |  F  |  F  |  F  |  F  |  F  |  F  |  F  |  F  |",
                "|                |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |  D  |",
                "|                |  S  |  S  |  S  |  S  |  S  |  E  |  E  |  E  |  E  |  E  |  E  |  B  |  B  |  B  |",
                "|                |  O  |  O  |  O  |  O  |  O  |  V  |  V  |  V  |  V  |  V  |  V  |  A  |  A  |  A  |",
                "|                |  A  |  A  |  A  |  A  |  A  |  F  |  F  |  F  |  F  |  F  |  F  |  Q  |  Q  |  Q  |",
                "|                |  K  |  K  |  K  |  K  |  K  |  B  |  C  |  F  |  G  |  H  |  I  |  A  |  A  |  A  |",
                "|                |  1  |  5  |  6  |  7  |  8  |     |     |     |     |     |     |  1  |  2  |  3  |");

my @info = (    "+----------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+",
                "| LHADHRDT       |     |     |     |     |     |     |     |     |     |     |     |     |     |    1|",
                "+----------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+",
                "| LHBAERDT       |     |    4|     |    4|     |     |     |     |     |     |     |     |     |     |",
                "+----------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+",
                "| LHEE1RDT       |     |     |   13|     |     |     |     |     |   48|     |     |     |     |     |",
                "+----------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+",
                "| LHLM2RDT       |   96|     |     |     |     |     |     |     |     |     |     |     |     |     |",
                "+----------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+",
                "| LHLSERDT       |     |     |     |     |     |     |     |     |     |     |     |     |    7|     |",
                "+----------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+",
                "| LHLW1RDT       |     |     |     |     |     |     |     |     |     |     | 9304|     |     |     |",
                "+----------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+",
                "| LHUP1RDT       |     |     |     |     |  160|84385|     |     |     |  271|     |     |     |     |",
                "+----------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+");

my $mw = MainWindow->new ( -background => "GREY" );
$mw->title("What Gap Issues There Have Been");
$mw->resizable(0, 0);
$mw->focus;
$mw->geometry("600x400");

my $TA1F = $mw->Frame(-width=>5,-height=>5,-foreground=>"BLUE",-background=>"GREY",-borderwidth=>2,-relief=>'groove')->place(-x=>5,-y=>5);
my $TA1 = $TA1F->Scrolled( 'ROText', -scrollbars => 'se', -height => 10)->pack(-side => 'left');
$TA1->configure(-background => "GREY",-wrap=>"none");
$TA1->insert('end', "$_\n") foreach @headers;

my $TA2F = $mw->Frame(-width=>5,-height=>5,-foreground=>"BLUE",-background=>"GREY",-borderwidth=>2,-relief=>'groove')->place(-x=>5,-y=>200);
my $TA2 = $TA2F->Scrolled( 'ROText', -scrollbars => 'se', -height => 10)->pack(-side => 'left');
$TA2->configure(-background => "GREY",-wrap=>"none");
$TA2->insert('end', "$_\n") foreach @info;

$TA1->Subwidget("xscrollbar")->configure(-command => ['xview', $TA2]);
$TA2->Subwidget("xscrollbar")->configure(-command => ['xview', $TA1]);

$mw->focus;
MainLoop;
exit 0;

      

+2


a source to share


1 answer


This is certainly possible. Use the scrollbar button -command

to call the procedure. In this procedure, call yview

on each text area you want to move.

UPDATE

When I wrote my original answer, I didn't read carefully enough to see that you are using Perl / Tk - I assumed Tcl / Tk. However, the same principle applies.



The following code replaces what you have below the first $mw->focus;

- it uses a single horizontal scrollbar to control two text widgets.

my $horiz = $mw->Scrollbar(-orient => 'horizontal');

my $f1 = $mw->Frame();
my $vert1 = $f1->Scrollbar();
my $text1 = $f1->ROText(
                    -height => 10, -wrap => 'none',
                    -yscrollcommand => [set => $vert1],
                    -xscrollcommand => [set => $horiz],
                );
$text1->insert('end', "$_\n") foreach @headers;
$text1->pack(-side => 'left');

$vert1->configure(-command => [yview => $text1]);
$vert1->pack(-side => 'left', -fill => 'y', -expand => 1);

my $f2 = $mw->Frame();
my $vert2 = $f2->Scrollbar();
my $text2 = $f2->ROText(
                    -height => 10, -wrap => 'none',
                    -yscrollcommand => [set => $vert2],
                    -xscrollcommand => [set => $horiz],
                );
$text2->insert('end', "$_\n") foreach @info;
$text2->pack(-side => 'left');

$vert2->configure(-command => [yview => $text2]);
$vert2->pack(-side => 'left', -fill => 'y', -expand => 1);

$horiz->configure(-command => sub { $text1->xview(@_); $text2->xview(@_) });

$f1->pack;
$f2->pack;
$horiz->pack(-fill => 'x', -expand => 1);

MainLoop;
exit 0;

      

+4


a source







All Articles