2015-04-15 60 views
2

我需要检查一个字符串(输入框上的用户输入)是否为空,并且在我按下“播放”按钮时根据该条件执行一些操作。 这是我的脚本:如何检查字符串是否为空

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"} 
        } 

但是,当我按下“播放”按钮,这是错误:

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) 

就如何解决它的任何想法?

回答

5

的问题是,你没有通过足够的参数来if

if {$Input ne ""} 

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

尝试使用

if {$Input ne ""} then { 
    puts "string is not empty" 
} else { 
    puts "string is empty" 
} 
+1

有些人喜欢在他们的'if's和'while's使用反斜杠换行。我讨厌那个,但有些人坚持。 –

+1

请注意,'then'关键字参数是可选的,通常省略。 –

+1

'else'关键字也是可选的,但为了清楚起见,通常添加。 –