https://www.cnblogs.com/yeungchie/

Callback 函数

proc Callback { handle } {
    variable line
    if { [catch {gets $handle line}] || [chan eof $handle]} {
        # 这里 gets 当获取失败,或者遇到 End Of File 时,关闭管道
        catch {close $handle}
    } else {
        puts "$line"
        flush stdout
    }
}

先创建一个函数用来接受返回值

启动外部程序

set fh [open "| python SyncViewConfigure.py" r]

这里运行外部程序,并返回 $fh

设置 $fh 为非阻塞模式

chan configure $fh -blocking 0

注册事件触发 Callback 函数

chan event $fh readable [list Callback $fh]

函数输入值为 $fh