我正在构建一个代码来将用户输入添加到文件中,但我想捕获一个用户只输入空格而不输入任何其他内容的事件.我怎么做?目前我正在硬编码""和"",如果用户输入一个空格或两个空格,它会被捕获,但我相信有一个比我更好的解决方案.
Proc将用户输入插入文本文件
proc inputWords {entryWidget} {
set inputs [$entryWidget get]
$entryWidget delete 0 end
if {$inputs == ""} {
.messageText configure -text "No empty strings"
} elseif {$inputs == " " || $inputs == " "} {
.messageText configure -text "No whitespace strings"
} else {
set sp [open textfile.txt a]
puts $sp $inputs
close $sp
.messageText configure -text "Added $inputs into text file."
}
}
Run Code Online (Sandbox Code Playgroud)
GUI代码
button .messageText -text "Add words" -command "inputWords .ent"
entry .ent
pack .messageText .ent
Run Code Online (Sandbox Code Playgroud)
接受任意长度的空格字符串,包括0:
string is space $inputs
Run Code Online (Sandbox Code Playgroud)
要接受非空的空格字符串:
string is space -strict $inputs
Run Code Online (Sandbox Code Playgroud)
结果为true(= 1)或false(= 0).
文档:字符串