how do i use "tkinter.createfilehandler" with a regular c program?

Jo Schambach jschamba at physics.utexas.edu
Mon Nov 14 18:36:48 CET 2005


I am trying to write a GUI with tkinter that displays the stdout from a
regular C/C++ program in a text widget.
The idea i was trying to use was as follows:

1) use "popen" to execute the C/C++ program
2) then use "tkinter.createfilehandler" to create a callback that  would
be called when the C/C++ program creates output on stdout.

Somehow, I can't get this to  work. here is what I have tried so  far:

import sys,os
from Tkinter import *

root = Tk()
mainFrame = Frame(root)
textBox = Text(mainFrame)
textBox.pack(fill=BOTH, expand=YES)
mainFrame.pack(fill=BOTH, expand=YES)

fh = os.popen('/homes/jschamba/tof/pcan/pcanloop')

def readfh(filehandle, stateMask):
    global textBox
    newText = filehandle.read()
    textBox.insert(END, newText)

tkinter.createfilehandler(fh, tkinter.READABLE, readfh)
root.mainloop()


I don't see any of the stdout from my program appear in the textbox.

Does anyone have a short example that I could use as an inspiration for
this task?

I guess what my ultimate goal would be is to create something similar to
the "expectk" call "expect_background", which does exactly what i just
described, i.e. wait for output from a shell/C/C++ program and then do
something in response to this output like insert it into a text widget.
In expect, the following program seems to work:

#!/usr/bin/expectk -f

# disable terminal output
log_user 0

spawn -noecho /homes/jschamba/tof/pcan/pcanloop
set shell $spawn_id
text .shell -relief sunken -bd 1 -width 90 -height 24 -yscrollcommand
{.scroll set}
scrollbar .scroll -command {.shell yview}
pack .scroll -side right -fill y
pack .shell -side bottom -expand true -fill both

expect_background {
    -i $shell -re "\[^\x0d]+" {
	.shell insert end $expect_out(0,string)
	.shell yview -pickplace insert
    }
}


Jo
-- 
Dr Joachim Schambach
The University of Texas at Austin
Department of Physics
1 University Station C1600
Austin, Texas 78712-0264, USA
Phone: (512) 471-1303; FAX: (814) 295-5111
e-mail: jschamba at physics.utexas.edu



More information about the Python-list mailing list