用户界面 - tk_optionMenu错误"can't read " ": no such variable"



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

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

当我按下Run按钮上的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)

有什么建议吗?

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

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

以下是全球手册页的摘录:

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

最新更新