如何为多个列表框(TCL)实现tk滚动条?

时间:2012-07-17 06:57:56

标签: windows 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 ";

感谢你。

3 个答案:

答案 0 :(得分:0)

如果你打算在callback命令中做很多工作 - 制作一个程序来执行它,因为它更快(程序得到字节编译)并且不太可能引入tcl语法问题。在这种情况下,您尝试在滚动条功能中执行两个tcl命令,因此您需要使用换行符或分号分隔语句。

从两个列表框中调用滚动条设置功能只会使第二个覆盖第一个。您需要一个函数来合并这两个,或者如果列表具有相同的长度,只需从其中一个调用它来设置滚动条大小和位置,然后使用滚动条回调更新所有列表框。

在某处有一个multilistbox包 - 尝试使用Tcl wiki查找示例。

答案 1 :(得分: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进行水平滚动,以及滚动条方向的更改)。此外,连接器 here 与wiki页面上的连接器不同,可以同时与多组滚动小部件一起使用;滚动内容的知识存储在滚动条的-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}
}

其他一切都是一样的。

答案 2 :(得分:0)

我知道这篇文章确实很老,但是最近我发现我认为这是一个非常简单的解决方案。我发现可以使用滑块控件而不是使用垂直滚动条。使用滑块,您可以获取滑块的位置,并使用它来设置列表框的yview。多个列表框可以同时滚动。我使用vtcl来构建GUI,因此对于使用tk wm comand的人来说,我可以提供的代码可能不是立即显而易见的。但是这是我使用的代码。它绑定到滑块运动。

Received response from agent with body: HTTP/1.1 200 OK Server: nginx/1.13.6 Date: Wed, 10 Oct 2018 08:15:48 GMT Content-Type: application/json;charset=UTF-8 Content-Length: 534 X-Cloud-Trace-Context: 6441a38beda637638dfd2e6b1f3e9c8a/7766163845645599757;o=0 Google-Actions-API-Version: 2 Via: 1.1 google Alt-Svc: clear 

{
  "conversationToken": "[]",
  "expectUserResponse": true,
  "expectedInputs": [
    {
      "inputPrompt": {
        "richInitialPrompt": {
          "items": [
            {
              "simpleResponse": {
                "textToSpeech": "text request"
              }
            }
          ]
        }
      },
      "possibleIntents": [
        {
          "intent": "assistant.intent.action.TEXT"
        }
      ],
      "speechBiasingHints": [
        "$measureUnit",
        "$product"
      ]
    }
  ],
  "responseMetadata": {
    "status": {
      "code": 14,
      "message": "Webhook error (206)"
    },
    "queryMatchInfo": {
      "queryMatched": true,
      "intent": "d060e25c-2e4e-4c7e-903b-5cb55c13a6f0",
      "parameterNames": [
        "product",
        "quantity",
        "measureUnit"
      ]
    }
  }
}

希望对某人有帮助。