2008年10月5日星期日

perl tk笔记: Text widget

创建:
$text = $parent->Text( [ options ... ] )->pack;

插入内容
$text->insert('end', "To be or not to be...\nThat is the question");
最常见的用法是创建一个Text widget,把从文件读来的内容置于其中
$text = $mw->Scrolled("Text")->pack( );
open (FH, "chapter1") || die "Could not open chapter1";
while () {
$text->insert('end', $_);
}
close(FH);

一些options

line spacing
-spacing1:指定新段落前空出行的大小
-spacing2:如果一行文字超过text widget大小,则自动换行,本选项指定行与行之间的间隔
-spacing3:段落结束后行空间大小

tabs
定制tab的大小
如 -tabs =>[2,'center'] 两字符大小,后面的center是什么意思没搞明白

文本索引
定位text widget中文本位置的方法

基本索引值
n.m 第n行第m个字符,注意字符从零开始
@x.y 离坐标x,y最近的字符
end 文本结尾,\n后面
mark
tag.first
tag.last
$widget
$image: 以上两个代表widget和image的位置

索引修饰符·
+ |
- ] n [ chars | lines ]
+-n字符或行
linestart
行首
lineend
行尾
wordstart
单词开始字符
wordend
单词结束字符

关于tag
tag是预先设置的文本属性,在插入文本的时候可以使用它
一个例子:
$t = $mw->Text()->pack( );
$t->tagConfigure('bold', -font => "Courier 24 bold");
$t->insert('end', "This is some normal text\n");
$t->insert('end', "This is some bold text\n", 'bold');
添加tag
$t->tagAdd('tagName','startPosition1','endPosition1','startPositon2','endPostion2',.....);
如:
$t->tagAdd('bold','sel.first','sel.last') if $t->tagRanges('sel');
sel是内置的特殊tag
sel.first/sel.last表示当前选中文本的开始和结束位置,我们使用tagRanges('sel')来判断是否有被选中的文本。

获得当前选中文本
$sl = $t->get('sel.first','sel.last') if $t->tagRanges('sel');
或者
$sl = $t->getSelected();

没有评论: