我正在建立一個代碼,以添加用戶輸入到一個文件,但我想捕捉一個事件,其中用戶只輸入空白,沒有別的。我怎麼做?目前我是硬編碼「」和「」,如果用戶輸入一個空格或兩個空白符號,它會被捕獲,但我相信有比我更好的解決方案。TCL檢查只有空格
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."
}
}
GUI代碼
button .messageText -text "Add words" -command "inputWords .ent"
entry .ent
pack .messageText .ent
這是測試對這種事情的規範的方法。 –