elseif not linestop loop at end-of-file output write(termelse display next line return output write(lineroot after( lambdaconsumer(outputrootterm)def redirectedguishellcmd(commandroot)input os popen(command' 'output guioutput(rootthread start_new_thread(producer(input,)consumer(outputrootstart non-gui program start reader thread if __name__ ='__main__'win tk(redirectedguishellcmd('python - pipe-nongui py'winwin mainloop(as usualwe use queue here to avoid updating the gui except in the main thread note that we didn' need thread or queue in the prior section' socket examplejust because we're able to poll socket to see whether it has data without blockingan after timer loop was enough for shell-command pipethougha thread is an easy way to avoid blocking when runthis program' self-test code creates scrolledtext window that displays the current date and time sent from the pipes-nongui py script in example - in factits window is identical to that of the prior versions (see figure - the window is updated with new line every two seconds because that' how often the spawned pipes-nongui script prints message to stdout note how the producer thread calls readline(to load just one line at time we can' use input calls that consume the entire stream all at once ( read()readlines())because such calls would not return until the program exits and sends end-of-file the read(ncall would work to grab one piece of the output as wellbut we assume that the output stream is text here also notice that the - unbuffered stream flag is used here againto get output as it is producedwithout itoutput won' show up in the gui at all because it is buffered in the spawned program (try it yourselfsockets and pipescompare and contrast let' see how we've done this script is similar in spirit to what we did in example - because of the way its code is structuredthoughexample - has major advantagebecause input calls are spawned off in thread this timethe gui is completely responsive window movesresizesand so forthhappen immediately because the gui is not blocked while waiting for the next output from the non-gui program the combination of pipethreadand queue works wonders here--the gui need not wait for the spawned programand the spawned thread need not update the gui itself more ways to add guis to non-gui code