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