开发者

How can I create 2 Perl/TK text areas with scrollbars that when I scroll one both text areas scroll at the same time?

Is it possible using TK to create to text areas with scrollbars that when you scroll one the other moves as well?

What I want to create is a text area with headers in and then text areas underneath row headings in one and data in the other. Kind of Like when you freeze panes in Excel. I have the data in a set of arrays for each line, so all i need is a way of linking the scrollbars in each of the text areas so the up down one in the data also controls the row headings and vice versa, and the left right one of the data controls the column headings and again vice versa.

Probably not possible but doesn't hurt to ask

EDIT

Ok so i have got some code and it almost does what i want but i need some help getting it to work completely. The code example shows that if you move one scrollbar it indeed controls the other text area, and vice versa, but it doesnt control its own text area, is there a way of adding multiple xviews to a command so it moves both textareas at 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|     |     |     |     |",
                "+----------------+-----+-----+-----+-----开发者_Go百科+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+");

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;


It's certainly possible. Use the scrollbar's -command option to invoke a procedure. In that 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 you were using Perl/Tk -- I assumed Tcl/Tk. Nevertheless, 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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜