zzari
zzari

Reputation: 313

How to check if a string is empty

I need to check if a string (user input on an entry box) is empty or not and do some action based on that condition once I push the "Play" button. This is my script:

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

But when I push the "Play" button this is the error:

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)

Any idea on how to fix it?

Upvotes: 3

Views: 5039

Answers (1)

Johannes Kuhn
Johannes Kuhn

Reputation: 15163

The problem is that you don't pass enough arguments to if

if {$Input ne ""}

Is not valid. In Tcl commands end with a newline.

Try to use

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

Upvotes: 7

Related Questions