如何为多个列表框(TCL)实现 tk 滚动条? [英] How to implement tk scrollbar for multiple listboxes (TCL)?
问题描述
我尝试了各种选项,但没有成功为两个或多个列表框实现简单的滚动条.以下是我的代码在滚动时出错.我希望你们能帮助我...
I tried all sort of options but no success in implement simple scrollbar for two or more listboxes. Following is my code giving error while scrolling. I hope you guys are helping me...
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 ";
谢谢.
推荐答案
Tcler 上有许多示例wiki,但核心原理是使用一个程序来确保小部件之间的滚动协议是同步的.这是基于该 wiki 页面的示例:
There are a number of examples on the Tcler's wiki, but the core principle is to use a procedure to ensure that the scrolling protocol is synchronized between the widgets. Here's an example based off that wiki page:
# 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
回调的第一个参数).
It's worth noting that you can also plug any other scrollable widget into this scheme; everything in Tk scrolls the same way (except with -xscrollcommand
and xview
for horizontal scrolling, together with a change of scrollbar orientation). Furthermore, the connectors here, unlike the ones on the wiki page, can be used with multiple groups of scrolled widgets at once; the knowledge of what is scrolled together is stored in the -command
option of the scrollbar (first argument to synchScroll
callback).
:对于 8.4 及更早版本,您需要稍微不同的连接器程序:
: For 8.4 and before, you need slightly different connector procedures:
# 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}
}
其他一切都一样.
这篇关于如何为多个列表框(TCL)实现 tk 滚动条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!