tk_optionMenu错误“无法读取”一个“:没有这样的变量”

时间:2014-04-24 08:32:42

标签: user-interface tcl tk

我正在尝试执行一个简单的代码

global a
eval tk_optionMenu .qt.oc a  [list 1 2 4 8 16]
proc Run {} {
    puts "$a"
}

当我在运行按钮上按下pres时,我有一个与Run proc关联的按钮 我收到以下错误:

can't read "a": no such variable
can't read "a": no such variable
    while executing
"puts "$a""
    (procedure "Run" line 2)
    invoked from within
"Run"
    invoked from within
".top.run invoke"
    ("uplevel" body line 1)
    invoked from within
"uplevel #0 [list $w invoke]"
    (procedure "tk::ButtonUp" line 22)
    invoked from within
"tk::ButtonUp .top.run"
    (command bound to event)

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

global必须在您尝试访问全局变量的范围内使用 。例如:

proc Run {} {
    global a
    puts "$a"
}

此处摘录自global man page

  

除非在proc的上下文中执行,否则此命令无效   体。