line self buff while linetext text line line self inputline(return text def readline(self)text self buff or self inputline(self buff 'return text def readlines(self)lines [while truenext self readline(if not nextbreak lines append(nextreturn lines def redirectedguifunc(func*pargs**kargs)import sys savestreams sys stdinsys stdout sys stdin guiinput(sys stdout guioutput(sys stderr sys stdout result func(*pargs**kargssys stdinsys stdout savestreams return result def redirectedguishellcmd(command)import os input os popen(command' 'output guioutput(def reader(inputoutput)while trueline input readline(if not linebreak output write(linereader(inputoutputread all till eof until cancel=eof='emulate file read methods read all lines map func streams to pop ups pops up dialog as needed new output window per call this is blocking call show shell command' standard output in new pop-up text box widgetthe readline call may block if __name__ ='__main__'def makeupper()while truetryline input('line'exceptbreak print(line upper()print('end of file'self test when run use standard streams def makelower(inputoutput)while trueline input readline(if not linebreak use explicit files guistreamsredirecting streams to widgets