如何将textvariable传递给文件

时间:2017-05-15 13:58:40

标签: tcl tk

我正在研究TCL / TK,任何人都可以告诉我如何将文本变量传递到文件中?

我尝试使用命令

将textvariable直接传递到文件中
puts $fo myvariable

它只将my变量保存到文件中。

2 个答案:

答案 0 :(得分:1)

如果您想在文件为<rewrite> <rules> <rule name="Main Rule" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="/" /> </rule> </rules> </rewrite> d时在全局变量中显示值,请在执行source之前设置变量:

source

如果从程序中执行此操作,则需要更加小心:

set myvariable "abc 123 xyz"
source thefile.tcl

如果您想要替换文件中的变量,则可以使用:

proc example {variableValue fileName} {
    global myvariable
    set myvariable $variableValue
    uplevel "#0" [list source $fileName]
}
# Call like this: example "abc 123 xyz" thefile.tcl

答案 1 :(得分:0)

我可以让你的代码有效,但我不喜欢它。

global myvariable
grid [label .myLabel -text "Label Widget" -textvariable labelText]
grid [text .myText -width 20 -height 5]
.myText insert 0.0 "Text\nWidget\n"
grid [entry .myEntry -background red -textvariable myvariable -foreground white]
grid [message .myMessage -background red -foreground white -text value]
grid [button .myButton1 -text "Button" -command "click"]
proc click {} {
  variable myvariable
  set fo [open "testo.txt" "w+"]
  toplevel .lvl2
  entry .lvl2.entry1 -textvariable myvariable
  puts -nonewline $fo $myvariable
  pack .lvl2.entry1
  close $fo
  exit
}

您的顶级.lvl2没用,因为您的应用程序将在按钮点击时退出 也许现在是时候阅读更多关于tcl基础知识了。