Tcl/Tk : Execute tcl script using tk button

389 Views Asked by At

Let say, there is a.tcl that contains infinite loop condition, e.g.,

while {1} {
    puts $val 
}

I want to implement a tk-button, that executes a.tcl file and continues to run and print $val in tk-text window at regular interval of time say every 1sec. Also, when we click on that button again, it should stop running a.tcl

Note : I tried using exec tclsh a.tcl but it hangs the tk-window because of infinite while loop in a.tcl

1

There are 1 best solutions below

0
On

If, instead of using exec, you launch the subprocess with:

set pipeline [open |[list tclsh a.tcl]]

then the GUI will remain active. However, you'll probably want to read from the pipeline from time to time.

proc line_per_second {pipeline} {
    puts [gets $pipeline]
    after 1000 line_per_second $pipeline
}
line_per_second $pipeline

When you close $pipeline, it should shut things down (because the OS pipe gets closed).

Note that for real code, as opposed to code which is just spewing the same line over and over as fast as possible, you can use a readable fileevent to trigger your call to gets instead.

proc read_line {pipeline} {
    gets $pipeline line
    if {[eof $pipeline]} {
        close $pipeline
    } else {
        puts $line
    }
}
fileevent $pipeline readable [list read_line $pipeline]

However, with your specific script that might trigger too often and starve the GUI of events a bit. It's only really a problem with very large amounts of output.