the Tcler's wiki上有很多例子,但核心原则是使用一个过程来确保滚动协议在小部件之间同步。下面是基于关闭该wiki页面的例子:
# Some data to scroll through
set ::items [lrepeat 10 {*}"The quick brown fox jumps over the lazy dog."]
# Some widgets that will scroll together
listbox .list1 -listvar ::items -yscrollcommand {setScroll .scroll}
listbox .list2 -listvar ::items -yscrollcommand {setScroll .scroll}
scrollbar .scroll -orient vertical -command {synchScroll {.list1 .list2} yview}
# The connectors
proc setScroll {s args} {
$s set {*}$args
{*}[$s cget -command] moveto [lindex [$s get] 0]
}
proc synchScroll {widgets args} {
foreach w $widgets {$w {*}$args}
}
# Put the GUI together
pack .list1 .scroll .list2 -side left -fill y
值得一提的,你也可以插入任何其他滚动部件这一计划; Tk中的所有内容都以相同的方式滚动(除了用于水平滚动的-xscrollcommand
和xview
以及滚动条方向的更改外)。此外,连接器这里,与维基页面上的连接器不同,可以一次与多组滚动的小部件一起使用;关于滚动到一起的知识存储在滚动条的-command
选项(synchScroll
回调的第一个参数)中。
[编辑]:对于8.4之前,你需要稍微不同的连接方法:
# The connectors
proc setScroll {s args} {
eval [list $s set] $args
eval [$s cget -command] [list moveto [lindex [$s get] 0]]
}
proc synchScroll {widgets args} {
foreach w $widgets {eval [list $w] $args}
}
一切将是相同的。
谢谢......我认为这适用于tcltk 8.5而我正在8.4(仍然)工作,所以不适合我。但我试图用你的基础开始。 – OliveOne 2012-07-17 14:18:47