2012-07-17 105 views
1

我嘗試了各種選項,但沒有成功實現兩個或多個列表框的簡單滾動條。以下是我的代碼在滾動時給出錯誤。我希望你們幫助我...如何實現多個列表框(TCL)的tk滾動條?

scrollbar .scroll -orient v 
pack .scroll -side left -fill y 
listbox .lis1 
pack .lis1 -side left 
listbox .lis2 
pack .lis2 -side left 

for {set x 0} {$x < 100} {incr x} { 
.lis1 insert end $x 
.lis2 insert end $x 
} 
.lis1 configure -yscrollcommand [list .scroll set] 
.lis2 configure -yscrollcommand [list .scroll set] 
.scroll configure -command ".lis1 yview .lis2 yview "; 

謝謝你。

回答

0

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中的所有內容都以相同的方式滾動(除了用於水平滾動的-xscrollcommandxview以及滾動條方向的更改外)。此外,連接器這裏,與維基頁面上的連接器不同,可以一次與多組滾動的小部件一起使用;關於滾動到一起的知識存儲在滾動條的-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} 
} 

一切將是相同的。

+0

謝謝......我認爲這適用於tcltk 8.5而我正在8.4(仍然)工作,所以不適合我。但我試圖用你的基礎開始。 – OliveOne 2012-07-17 14:18:47

0

如果你打算在回調命令中做很多工作 - 建立一個過程來做到這一點,因爲這樣做速度更快(過程獲得字節編譯)並且不太可能引入tcl語法問題。在這種情況下,您正試圖在滾動條函數中執行兩個tcl命令,因此您需要使用換行符或分號分隔語句。

從兩個列表框中調用滾動條設置功能將只是第二個覆蓋第一個。您可能需要一個函數來合併這兩個列表,或者如果列表具有相同的長度,只需從其中一個調用它來設置滾動條的大小和位置,然後使用滾動條回調更新所有列表框。

某處有一個multilistbox包 - 請嘗試使用Tcl wiki來查找示例。

+0

感謝「patthoyts」......我希望詳細瞭解這一點,而不僅僅是完成它。 – OliveOne 2012-07-17 13:56:21