[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

text = '' # read all till eof
line = self.buff
while line:
text = text + line
line = self.inputLine() # until cancel=eof=''
return text


def readline(self):
text = self.buff or self.inputLine() # emulate file read methods
self.buff = ''
return text


def readlines(self):
lines = [] # read all lines
while True:
next = self.readline()
if not next: break
lines.append(next)
return lines


def redirectedGuiFunc(func, *pargs, *kargs):
import sys
saveStreams = sys.stdin, sys.stdout # map func streams to pop ups
sys.stdin = GuiInput() # pops up dialog as needed
sys.stdout = GuiOutput() # new output window per call
sys.stderr = sys.stdout
result = func(
pargs, **kargs) # this is a blocking call
sys.stdin, sys.stdout = saveStreams
return result


def redirectedGuiShellCmd(command):
import os
input = os.popen(command, 'r')
output = GuiOutput()
def reader(input, output): # show a shell command's
while True: # standard output in a new
line = input.readline() # pop-up text box widget;
if not line: break # the readline call may block
output.write(line)
reader(input, output)


if name == 'main': # self test when run
def makeUpper(): # use standard streams
while True:
try:
line = input('Line? ')
except:
break
print(line.upper())
print('end of file')


def makeLower(input, output): # use explicit files
while True:
line = input.readline()
if not line: break


GuiStreams: Redirecting Streams to Widgets | 625
Free download pdf