如何解决运行 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]
在这种情况下,使用哪个并不重要。 (前两个是等价的,除了空格。当您在脚本中绑定变量时,第三个更有用,但在这种情况下您没有这样做。)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。