在 Win 10 上,Tcl/Tk 脚本2个示例
set PATH 新增 D:\Git\mingw64\bin
where tclsh
D:\Git\mingw64\bin\tclsh.exe
where wish
D:\Git\mingw64\bin\wish.exe
编写 test_tk.tcl 如下
#!/usr/bin/tclsh
# test 文件对话框
package require Tk# 弹出文件选择对话框,限制选择.txt文件
set filePath [tk_getOpenFile -title "选择文件" \-filetypes {{"文本文件" {.txt}} {"所有文件" {*.*}}}]# 检查用户是否选择了文件
if {$filePath != ""} {exec notepad.exe $filePath
} else {tk_messageBox -title "Error" -message "没有选择文件!" -icon error
}
运行 tclsh test_tk.tcl
或者 wish test_tk.tcl
编写 open_video.tcl 如下
#!/usr/bin/tclsh
# test 文件对话框
package require Tk# 弹出文件选择对话框,限制选择.avi文件
set dir1 "D:/VIDEO"
set filePath [tk_getOpenFile -title "选择文件" \-filetypes {{"AVI文件" {.avi}} {"All Files" {*.*}}} \-initialdir $dir1 ]# 检查用户是否选择了文件
if {$filePath != ""} {exec "C:/Program Files/Windows Media Player/wmplayer.exe" $filePath
} else {tk_messageBox -title "Error" -message "没有选择文件!" -icon error
}
运行 wish open_video.tcl
注意:脚本编码需要转为ANSI/GBK,如果是UTF-8,中文显示会乱码。