I am trying to route the output of a separate script to a Tkinter window.
Below is my attempt at the problem. The Tkinter box appears but does not update when the controller (abstracted here) writes to pipeout using os.write(pipeout, msg) .
from Tkinter import *
from controller import controller
import os
def run():
"""Top level run method which initiates program."""
def updateInput():
"""Update the textbox with controller output."""
readFrom = os.fdopen(pipein)
line = readFrom.readline()
text.insert(END, line)
text.after(1000, updateInput)
pipein, pipeout = os.pipe() #pipe for comms between tkinter and controller
pid = os.fork()
if not pid:
#within child process, launch controller with passed pipe
os.close(pipein)
mainController = controller(pipeout)
os.close(pipeout)
root = Tk()
text = Text(root)
text.pack()
text.after(1000, updateInput) #update text box each second
root.mainloop()
if __name__ == "__main__":
run()
The abstracted controller is writing to the pipe via
os.write(self.pipeout, msg)
where self.pipeout
has been assigned from self.pipeout = pipeout
in the controller class init .
Sounds like you forgot to flush.
self.pipeout.write(msg)
self.pipeout.flush()
Also, make sure the msg ends in a newline.
Edit: are you sure you need a pipe? There's probably neater ways to do whatever you are doing, like threading.
Solution came from using os.read(pipein, 100)
instead of os.fdopen(pipein)
in updateInput
.
line = os.read(pipein, 100)
text.insert(END, line)
text.after(1000, updateInput)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.