查看完整版本: Perl/Tk FAQ - 10.13. 如何在Text组件中获得某个标签的范围

有线电视 2007-11-25 18:18

Perl/Tk FAQ - 10.13. 如何在Text组件中获得某个标签的范围

  原文:

10.13. How do I select a range of tags in a Text widget?

A question arose concerning getting a range of selections from a Text widget. Nick Ing-Simmons' answer mentions several possibilities including:

Keyboard Copy/Paste 'is' implemented of course...

Subj: RE: $Text->tag('ranges', 'sel') - does this work?In <199512291957.OAA02609@ohm.nrl.navy.mil>On Fri, 29 Dec 1995 14:57:42 -0500Charles J Williams <chas@ohm.nrl.navy.mil> writes:!I was writing a little tk perl today, and i decided to try to !implement a copy/paste using the 'sel' tag!!I enabled exportselection, and then try to probe the select !region with:!! $buffer = $text->tag('ranges', 'sel');!!$buffer comes back with one entry, the end of the selection.

That is to be expected - the scalar gets assigned the last element of the list.

!I tried:!! @buffer = $text->tag('ranges', 'sel');!!same difference.

This seems to work for me:

($start,$end) = $text->tagRanges('sel');

In perl/Tk ->tagRanges(...) is an alias for ->tag('ranges',...)

The following subroutine can also probe and print the tagRanges:

sub showsel { my $text = @_; my @info = $text->tagRanges('sel'); if (@info) { print "start=$info[0] end=$info[1]\n" } }



译文:

10.13. 如何在Text组件中获得某个标签的范围?



这是一个关于在Text组件中如何获得所选择文字的范围的问题。下面就是作者Nick Ing-Simmons的回答:

键盘的复制和粘贴确实是可以实现的……



主题:回复:$Text->tag(‘ranges’,’sel’) – 可以吗?

在<199512291957.OAA02609@ohm.nrl.navy.mil>

On Fri, 29 Dec 1995 14:57:42 –0500

Charles J Williams写到:

!我今天在写一些Perl/Tk的脚本,其中我想试着用’sel’的标签来实现复制和粘贴的功能。

!我启用了portselection,然后试图用下面的方法来获得所选择的区域:



! $buffer = $text -> tag(‘ranges’,’sel’);



!但是,$buffer获得的只是一条记录——是选择区域的结束点。



这是意料之中的,因为标量只能获得列表的最后一个值。



!我又尝试了下面的方法:



! @buffer = $text -> $tag(‘ranges’,’sel’);



!有些不同。



我觉得这样应该可以:

($start,$end) = $text -> tagRanges(‘sel’);

注:在Perl/Tk中->tagRanges(…)与->tag(‘ranges’,…)是等价的。



下面的子程序也可以获取并输出tagRanges的结果:

sub showsel

{

my $text = @_;

my @info = $text->tagRanges('sel');

if (@info)

{

print "start=$info[0] end=$info[1]\n"

}

}
页: [1]

查看完整版本: Perl/Tk FAQ - 10.13. 如何在Text组件中获得某个标签的范围