2012-06-16 58 views

回答

0

您可以为您的文本处理程序使得:

class textbox_handler: 
    def __init__(self, text): 
    self.data = [] 
    self.text = text #text is your tk text widget. 
    def write(self, s): 
    self.data.append(s) 
    def print_out(self): 
    for line in data: 
     self.text.insert('end',line) 

然后,在你的主要功能:

import sys 
def main(): 
    handler = textbox_handler(text1) 
    sys.stdout = handler 
    print "sample text." 
    print "sample text #2." 
    print "another sample text." 
    #or you can use without setting sys.stdout 
    print >>handler, "yet another sample text." 

每个print语句调用写处理程序对象的方法。你可以简单地从handler.data中检索这些字符串。