PerlTkx ---- tcltk文本组件text

    xiaoxiao2021-03-25  142

    tcl/tk text组件命令解析

    使用text组件对文件编辑

    tcltk text组件命令解析 text命令创建文本组件文本索引文本标记 标记选项标记优先级标记绑定 搜索与替换虚拟事件撤销和重做同级文本组件嵌入式窗口和嵌入图像获取代码

    text命令创建文本组件

    text .text -relief raised -bg yellow -yscrollcommand {.scroll set} scrollbar .scroll -command {.text yview} grid .text -row 0 -column 0 -sticky news grid .scroll -row 0 -column 1 -sticky ns proc loadFile {} { set file [tk_getOpenFile -initialfile text] .text delete 1.0 end; # delete content in .text set f [open $file]; # read $file into $f .text insert end [read $f]; # close $f }

    文本索引

    文本中的位置说明符称为索引

    最简单的形式,一个点分开的两个数组组成,如2.3表示第二行字符索引是3的位置. 行号从1开始编号,一行中的字符从0开始编号.

    5.end表示代表第五行终止的换行符

    ## 删除一行 ## 5.0->5.end到删除一行但是不包括换行符,5.end再删除换行符 .text delete 5.0 5.end 5.end .text delete 5.0 6.0

    @x.y形式的索引中的数字x和y指在窗口中最靠近x,y坐标处像素的字符.

    对文本中表征位置的字符设定名称,称为设定记号

    .text mark set first 2.4 insert 识别插入光标的位置 .text delete insert "insert + 2 chars"

    文本标记

    文本标记提供了一种类似画布的标记机制,区别是文本标记作用于字符而不是图像. 文本组件中标记提供三种功能:

    它们可以用于调整字符的排版格式,如前景色 背景色 字体 间隔和左右页边距.它们提供了一种管理选择的方式.它们提供事件绑定功能,能将纯文本转换为活动的控件.

    标记的名称可是为任意字符串值,也能应用与文本中的任何范围 单个字符也可能有多个标记 单个标记可能会与多个字符关联

    .text tag add thisTag 1.0 1.end ## 将1.0-1.end标记thisTag .text tag ranges hot ## 返回所有标记为hot的字符的起始和末尾列表 .text delete 1.0 end ## 在text重新加载一个文件时,使用该命令把之前的内容删除 ## 1.0是1行0字符的位置,end是文章的结尾 .text delete first first + 4 chars

    其他详见tk/text文档

    标记选项

    标记选项大概24个,分为三类:

    用于隐藏文本,只有一个 elide 布尔值用于格式化文本 backgroundborderwidthfont…用于布局 justifytabs… ### 搜索文本row,改变背景色字体大小和浮雕 proc doChange {} { forAllMatches .text row { .text tag add big first last } .text tag configure big -background "blue" -font {30} -relief raised }

    标记优先级

    某个范围的字符能对应多个标记,对可能的冲突事件,需要根据优先级处理 新定义的标记,优先级最高 用raise提升优先级,用lower降低优先级

    .text tag raise thisTag .text tag lower thisTag thatTag

    标记绑定

    文本组件允许使用tag bind子命令关联绑定和标记. 可以使用绑定是部分文本处于激活状态,从而响应鼠标 键盘 <Enter> <Leave>或虚拟事件.

    proc doChange {} { forAllMatches .text row { .text tag add big first last } .text tag configure big -background "blue" -font {30} -relief raised .text tag bind big <Enter> { .text tag configure big -background "red" -font {40} } .text tag bind big <Leave> { .text tag configure big -background "blue" -font {30} -relief raised } }

    搜索与替换

    文本组件支持search子命令和replace子命令.

    ### 用记号和索引提供的一般性搜索功能 proc forAllMatches {w pattern script} { set countList [list] set startList [$w search -all -regexp -count countList $pattern 1.0 end] foreach first $startList count $countList { $w mark set first $first $w mark set last [$w index "$first + $count chars"] uplevel 1 $script } }

    forAllMatches总过有三个参数:文本组件名 正则表达式和脚本. 它使用search命令执行匹配并找出匹配文本的开始索引 -count选项为变量countList中的每一个匹配对象返回字符的个数 对每个匹配对象,forAllMatches设置记号first和last分别标记起始和末端,然后调用脚本

    ## 搜索文本p并打印起始和末端的索引 proc doSearch {} { forAllMatches .text p { puts "[.text index first]---->[.text index last]" } }

    虚拟事件

    文本组件产生两个虚拟事件

    <<Modified>>事件与撤销机制相关

    该特性需要将-undo设置为真来启用通常用于启用save特性

    <<Selection>>事件与选择有关

    当且仅当sel标记应用于任意字符时,它会启用接口特性,如剪切和复制,若没有应用,则它会禁用该特性

    撤销和重做

    文本组件有无限的撤销/重做机制,使用前要text的-undo特性启用 该机制记录了在一个堆栈上的每一个插入和删除操作,撤销和重做是以两个堆栈边界之间的内容为基本单元执行操作的

    ## bool设置为0,用来清除修改标识,通常文件加载后清除修改标识. .text edit modified ?bool? ## .text撤销 .text edit undo ## .text重做 .text edit redo ## 增加堆栈边界,通常一行一次修改会自动作为加载一个堆栈边界 .text edit separator proc undoText {tw} { catch {$tw edit undo} } proc redoText {tw} { catch {$tw edit redo} } proc separatorText {tw} { catch {$tw edit separator} } proc resetText {tw} { catch {$tw edit reset} } proc dataModified {tw} { if {[$tw edit modified]} { .undo configure -state normal .redo configure -state normal } else { .undo configure -state disabled .undo configure -state disabled } } button .undo -text undo -command {undoText .text} button .redo -text redo -command {redoText .text} button .separ -text sepStack -command {separatorText .text} button .reset -text resetUndo -command {resetText .text}

    同级文本组件

    peer文本组件命令用于创建文本组件副本,它们能共享相同的文本数据.一个文本组件中的任何改变都能立即出现在它的同级组件中. 同级组件共享所有的文本 图像和标记. 它们不共享嵌入式窗口 sel标记, 记号insert和current

    toplevel .shareText .text peer create .shareText.text -relief raised -bg green -yscrollcommand ".scroll2 set" -undo 1 scrollbar .scroll2 -command ".shareText.text yview" pack .shareText.text -in .shareText

    嵌入式窗口和嵌入图像

    ………………..

    获取代码

    https://github.com/guowenshuai/perl_program/blob/master/wish/eg1.tcl

    转载请注明原文地址: https://ju.6miu.com/read-14445.html

    最新回复(0)