Hi,
I redirect stdout to a file. Then I use Tcl events to know when the file is readable and when it changes. Finally you just have to write a callback which will fill the text widget with the content of the file.
I don't know if it's the best way to do that, but it works.
PS: Don't forget to unbuffer stdout (cf expect)
Example:
#######
# Set up to deliver file events on the pipe
fconfigure $fd -buffering line -blocking false -translation auto
fileevent $fd readable [list callback $fd $widg]
# Launch the event loop and wait for the file events to # finish
vwait :ONE
###
# And now the callback
####################
# fd is the file descriptor of the redirection of stdout
# Widg is the text widget
proc callback {fd widg} {
set status [catch {gets $fd line} result]
if { $status != 0 } {
# Error on the channel
$widg insert end "error reading $fd: $result"
set :ONE 2
} elseif { $result >= 0 } {
# Successfully read the channel
$widg insert end "$line\n"
$widg see end
} elseif { [eof $fd] } {
# End of file on the channel
close $fd
set :ONE 1
} elseif { [fblocked $fd] } {
# Read blocked. Just return
} else {
# Something else
tk_messageBox -title "Error" -message "can't happen!!!" -type "ok" -icon "error"
set :ONE 3
}
}
Well, you can also use Tcl_Eval, if you want to modify your C code with Tcl C procedure.
Tcl_Eval(interp, "text_widget_name insert end {what you wanna see in your widget}".
Actuall, Anto35, while the first technique you described sounds like a perfectly reasonable application of the fileevent command, it isn't. Contrary to its name, fileevent works properly with every type of channel except a file. Check out the Tcl'ers Wiki (
Your second suggestion is actually the best way to handle inserting data from a C/C++ program into a text widget -- Tcl_Eval() or one of its relatives. - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting,
Thanx a lot Avia...
I improved the execution speed of my application with Tcl threads, but I understand that I lost my time because of the fileevent mechanism. Now I use 'after 100' to know when I've got to read stdout and I've got any speed problem no more!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.