运行 tcl 脚本时出现“无效的命令名称“W””

问题描述

我在 ModelSim 的 TCL 上制作了一些 GUI,但是当我运行它时会导致一些奇怪的错误

# invalid command name "W"
#     while executing
# "$w nearest $y"
#     (procedure "ListSelectEnd" line 2)

这有点奇怪,因为几乎相似的表情就在那个之前。代码如下:

global a
proc ScrolledListBox { parent args } {
    frame $parent
    eval {listBox $parent.list \
        -yscrollcommand [list $parent.sy set] \
        -xscrollcommand [list $parent.sx set]} $args
    scrollbar $parent.sx -orient horizontal \
        -command [list $parent.list xview]
    scrollbar $parent.sy -orient vertical \
        -command [list $parent.list yview]
    pack $parent.sx -side bottom -fill x
    pack $parent.sy -side right -fill y
    pack $parent.list -side left -fill both -expand true
    return $parent.list
}
#-------------------------------------------
proc ListSelect { parent choices } {
    global a
    frame $parent
    ScrolledListBox $parent.choices -width 20 -height 5 \
        -setgrid true
    ScrolledListBox $parent.picked -width 20 -height 5 \
        -setgrid true
    pack $parent.choices $parent.picked -side left \
        -expand true -fill both
    bind $parent.choices.list <ButtonPress-1> \
        {ListSelectStart %W %y}
    bind $parent.choices.list <ButtonRelease-1> \
    lappend a [ListSelectEnd %W %y .top.f.picked.list]
    eval {$parent.choices.list insert 0} $choices
}
#----------------------------------------
proc ListSelectStart { w y } {
    $w select anchor [$w nearest $y]
}
#-----------------------------------------
proc ListSelectEnd { w y list } {
    $w select set anchor [$w nearest $y]
    foreach i [$w curselection] {
        $list insert end [$w get $i]
        lappend listin [$w get $i]
    }
    return $listin
}
#--------------------------------------------
proc tosignal {parent val} {
    global a
    for {set i 0} {$i<[llength $a]} {incr i} {
        force -freeze sim:/chema_tb/m1/[lindex $a $i] $val 0
    }
    run 1000 ns
    destroy $parent
    return 1
}
#------------------------------------------------
proc form {} {
    global a
    toplevel .top 
    set filename signalfile.txt
    set in [open $filename]
    while {[gets $in var]>-1} {
        lappend spisn [lindex $var 0]
    }
    ListSelect .top.f $spisn
    button .top.okb  -text  OK -width 20 -height 2 -font {-size 15 -family Times -weight bold} \
        -fg blue -anchor center  -command {tosignal .top 0 }
    pack .top.f .top.okb -expand true
}

如果您能帮助我,我将不胜感激。 :)

解决方法

问题在于这些行:

bind $parent.choices.list <ButtonRelease-1> \
lappend a [ListSelectEnd %W %y .top.f.picked.list]

这会立即执行看似回调的内容,并将非常 不太可能的参数传递给 bind。我敢打赌你想要的是:

bind $parent.choices.list <ButtonRelease-1> \
    {ListSelectEnd %W %y .top.f.picked.list}

或者这个:

bind $parent.choices.list <ButtonRelease-1> {
    ListSelectEnd %W %y .top.f.picked.list
}

或者这个:

bind $parent.choices.list <ButtonRelease-1> \
    [list ListSelectEnd %W %y .top.f.picked.list]

在这种情况下,使用哪个并不重要。 (前两个是等价的,除了空格。当您在脚本中绑定变量时,第三个更有用,但在这种情况下您没有这样做。)