您需要创建一个类似文件的类,其
write方法改为写入Tkinter小部件,然后执行
sys.stdout = <your newclass>。
示例(从链接复制):
class IORedirector(object): '''A general class for redirecting I/O to this Text widget.''' def __init__(self,text_area): self.text_area = text_areaclass StdoutRedirector(IORedirector): '''A class for redirecting stdout to this Text widget.''' def write(self,str): self.text_area.write(str,False)
然后,在您的Tkinter小部件中:
# To start redirecting stdout:import syssys.stdout = StdoutRedirector( self )# (where self refers to the widget)# To stop redirecting stdout:sys.stdout = sys.__stdout__
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)