Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据条目输入TCL删除文件中的字符串_Tcl_Tk - Fatal编程技术网

根据条目输入TCL删除文件中的字符串

根据条目输入TCL删除文件中的字符串,tcl,tk,Tcl,Tk,我制作了一个简单的程序,接受简短的用户输入并将其存储在一个文件中。另一个按钮将文件中的每个输入显示为一个按钮。我正在尝试创建另一个进程,当我单击由字符串生成的按钮时,该进程将删除该按钮以及文件中的字符串。我该怎么做?我尝试对保存值的变量regsub,但似乎删除了一次,而不是每次 获取当前目录的代码 catch { set abspath [file readlink [info script]]} if { ![info exists abspath]} { set abspath $argv

我制作了一个简单的程序,接受简短的用户输入并将其存储在一个文件中。另一个按钮将文件中的每个输入显示为一个按钮。我正在尝试创建另一个进程,当我单击由字符串生成的按钮时,该进程将删除该按钮以及文件中的字符串。我该怎么做?我尝试对保存值的变量
regsub
,但似乎删除了一次,而不是每次

获取当前目录的代码

catch { set abspath [file readlink [info script]]} 
if { ![info exists abspath]} { set abspath $argv0 }
if { [regexp {^(\S+)\/\S+} $abspath matched dir]} { set BIN $dir }
file mkdir $BIN/debug
if {[file exists $BIN/debug/debug.txt]} { close [open $BIN/debug/debug.txt "w"]}
GUI代码

label .lbl -text "Enter something"
entry .en -justify center
button .sub -text "SUBMIT" -command "submit .en"
button .sho -text "SHOW" -command sho
button .cl -text "CLEAR" -command clear
grid .lbl -columnspan 3
grid .en -columnspan 3
grid .sub .sho .cl
提交程序

proc submit {ent} {
global BIN
if {![file isdirectory $BIN/debug]} { file mkdir $BIN/debug }
set input [$ent get]
if {$input == "" || [string is space -strict $input]} {
$ent delete 0 end
.lbl configure -text "No empty strings"
} else {
set fp [open $BIN/debug/debug.txt a+]
$ent delete 0 end
puts $fp $input
close $fp
}
}
proc clear {} {
global BIN
if {[file exists $BIN/debug/debug.txt]} { close[open $BIN/debug/debug.txt "w"] } 
} 
proc sho {} {
global BIN 
global filedat 
set w.gui
if {[info exists filedat]} { set filedat "" }
toplevel $w
wm title "values"
wm overrideredirect $w 1 
bind $w <Button-3> "destroy $w"
if {[file exists $BIN/debug/debug.txt]} {
set fp [open $BIN/debug/debug.txt r]
while {[gets $fp data] > -1} {
lappend filedat $data
}
close $fp
if {[info exist filedat]} {
set dcount 0
foreach item $filedat {
button $w.bn$dcount -text "$item" -font [list arial 10] -anchor w -fg white -bg black -command "del $item"
grid $w.bn$dcount -sticky w
incr dcount
}
} else {
label $w.nthLabel -text "Nothing in file" -bg black -fg white
grid $w.nthLabel
}
}
}
清除程序

proc submit {ent} {
global BIN
if {![file isdirectory $BIN/debug]} { file mkdir $BIN/debug }
set input [$ent get]
if {$input == "" || [string is space -strict $input]} {
$ent delete 0 end
.lbl configure -text "No empty strings"
} else {
set fp [open $BIN/debug/debug.txt a+]
$ent delete 0 end
puts $fp $input
close $fp
}
}
proc clear {} {
global BIN
if {[file exists $BIN/debug/debug.txt]} { close[open $BIN/debug/debug.txt "w"] } 
} 
proc sho {} {
global BIN 
global filedat 
set w.gui
if {[info exists filedat]} { set filedat "" }
toplevel $w
wm title "values"
wm overrideredirect $w 1 
bind $w <Button-3> "destroy $w"
if {[file exists $BIN/debug/debug.txt]} {
set fp [open $BIN/debug/debug.txt r]
while {[gets $fp data] > -1} {
lappend filedat $data
}
close $fp
if {[info exist filedat]} {
set dcount 0
foreach item $filedat {
button $w.bn$dcount -text "$item" -font [list arial 10] -anchor w -fg white -bg black -command "del $item"
grid $w.bn$dcount -sticky w
incr dcount
}
} else {
label $w.nthLabel -text "Nothing in file" -bg black -fg white
grid $w.nthLabel
}
}
}
为文件中的每个项目生成按钮的过程

proc submit {ent} {
global BIN
if {![file isdirectory $BIN/debug]} { file mkdir $BIN/debug }
set input [$ent get]
if {$input == "" || [string is space -strict $input]} {
$ent delete 0 end
.lbl configure -text "No empty strings"
} else {
set fp [open $BIN/debug/debug.txt a+]
$ent delete 0 end
puts $fp $input
close $fp
}
}
proc clear {} {
global BIN
if {[file exists $BIN/debug/debug.txt]} { close[open $BIN/debug/debug.txt "w"] } 
} 
proc sho {} {
global BIN 
global filedat 
set w.gui
if {[info exists filedat]} { set filedat "" }
toplevel $w
wm title "values"
wm overrideredirect $w 1 
bind $w <Button-3> "destroy $w"
if {[file exists $BIN/debug/debug.txt]} {
set fp [open $BIN/debug/debug.txt r]
while {[gets $fp data] > -1} {
lappend filedat $data
}
close $fp
if {[info exist filedat]} {
set dcount 0
foreach item $filedat {
button $w.bn$dcount -text "$item" -font [list arial 10] -anchor w -fg white -bg black -command "del $item"
grid $w.bn$dcount -sticky w
incr dcount
}
} else {
label $w.nthLabel -text "Nothing in file" -bg black -fg white
grid $w.nthLabel
}
}
}

使用dep proc删除字符串时,您将新字符串保存在变量filedat2中

全局变量filedat从未更改

如果要从全局变量中删除字符串,则必须将此变量传递给regsub,而不是filedat2

或者,如果您希望将其保存在时间变量中以执行某些测试,则可以使用filedat2,然后再次分配该变量:

regsub -all $st $filedat "" filedat2
# ... the tests
if {[isOk]} {
    # update the variable
    set filedat $filedat2
} else {
    # leave the previous value
    puts "some error here"
}

通过使用
set BIN[file dirname$abspath]
,您可以更高效、更容易地执行第三行中的操作。要获得与Tcl中任意文本匹配的正则表达式,@DonalFellows非常感谢,我不知道这一点。实际上,我总是将regexp与regexp和regsub一起使用。