【问题标题】:How can I create 2 Perl/TK text areas with scrollbars that when I scroll one both text areas scroll at the same time?如何创建 2 个带有滚动条的 Perl/TK 文本区域,当我滚动一个时,两个文本区域同时滚动?
【发布时间】:2010-04-28 12:22:24
【问题描述】:

是否可以使用 TK 创建带有滚动条的文本区域,当您滚动一个滚动条时,另一个滚动条也会移动?

我要创建的是一个带有标题的文本区域,然后是行标题下方的文本区域,另一个是数据。当您在 Excel 中冻结窗格时,有点像。我在每行的一组数组中都有数据,所以我需要的是一种链接每个文本区域中的滚动条的方法,这样数据中的上下一个也控制行标题,反之亦然,左边右边的数据控制列标题,反之亦然。

可能不可能,但问一下也无妨

编辑

好的,所以我有一些代码,它几乎可以满足我的要求,但我需要一些帮助才能让它完全工作。代码示例显示,如果您移动一个滚动条,它确实控制另一个文本区域,反之亦然,但它不控制自己的文本区域,有没有办法将多个 xviews 添加到命令中,以便同时移动两个文本区域时间。提前致谢

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;

【问题讨论】:

    标签: perl textarea scrollbar tk


    【解决方案1】:

    这当然是可能的。使用滚动条的-command 选项来调用过程。在该过程中,在您要移动的每个文本区域上调用 yview

    更新

    当我写下我的原始答案时,我没有仔细阅读以看到您使用的是 Perl/Tk——我假设是 Tcl/Tk。然而,同样的原则也适用。

    以下代码替换了第一个 $mw->focus; 下方的代码——它使用单个水平滚动条来控制两个文本小部件。

    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;
    

    【讨论】:

    • 太棒了,非常感谢,我现在可以使用您提供的内容并将其扩展到我的需要,现在我有了原理。再次感谢您的时间和帮助
    猜你喜欢
    • 1970-01-01
    • 2014-12-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多