Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output to text widget from C++ program 1

Status
Not open for further replies.

edwong

Programmer
Mar 26, 2003
30
0
0
US
How do you output to to a text widget through a C++ program

Thanks
 
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 ::DONE

###
# 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 ::DONE 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 ::DONE 1
} elseif { [fblocked $fd] } {
# Read blocked. Just return
} else {
# Something else
tk_messageBox -title "Error" -message "can't happen!!!" -type "ok" -icon "error"
set ::DONE 3
}
}

I hope this will help!
 
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. [neutral] Check out the Tcl'ers Wiki ( at the bottom of the page "fileevent," for a discussion of this oddity.

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, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top