如何检查字符串是否为空

zza*_*ari 3 if-statement tk-toolkit tcl

我需要检查字符串(输入框上的用户输入)是否为空,并在按下"播放"按钮后根据该条件执行某些操作.这是我的脚本:

entry .eInput -width 50 -relief sunken -justify center -textvariable Input
button .bAction  -text "Play" -width 30 -height 5 -activebackground green -font "-12" 

bind .bAction <1> {
 if {$Input ne ""}
  { puts "string is not empty"}
 else { puts "string is empty"}
                  }
Run Code Online (Sandbox Code Playgroud)

但是当我按下"播放"按钮时,这就是错误:

wrong # args: no script following "$Input ne """ argument
wrong # args: no script following "$Input ne """ argument
    while executing
"if {$Input ne ""}"
    (command bound to event)
Run Code Online (Sandbox Code Playgroud)

有关如何解决它的任何想法?

Joh*_*uhn 5

问题是你没有传递足够的参数 if

if {$Input ne ""}
Run Code Online (Sandbox Code Playgroud)

无效.在Tcl命令中以换行符结束.

尝试使用

if {$Input ne ""} then {
    puts "string is not empty"
} else {
    puts "string is empty"
}
Run Code Online (Sandbox Code Playgroud)