【发布时间】:2013-12-16 16:26:32
【问题描述】:
只有当您在重定向sys.stdout 后打印某些内容时,我的打印无限数字集的程序才能按预期工作,否则程序会冻结,这是为什么呢?这是一个错误吗?
代码:
import Tkinter as Tk
import sys
import threading
def func():
i = 0
while True:
i +=1
print i
class Std_redirector(object):
def __init__(self,widget):
self.widget = widget
def write(self,string):
self.widget.see(Tk.END)
self.widget.insert("end",string)
root = Tk.Tk()
text = Tk.Text(root)
text.pack()
sys.stdout = Std_redirector(text) #Redirect stdout to Tkinter text widget
#print 'hey' #If you uncomment this line, the program works!
thread1 = threading.Thread(target=func)
thread1.start() #Starts printing
root.mainloop()
如果您未注释此行,则此脚本有效:print 'hey'
顺便说一句,我的操作系统是 windows 7
【问题讨论】:
-
它适用于我 - 直接在 Python 中。在类似于
IDLE的DreamPiepython shell 中,我收到错误'Std_redirector' object has no attribute 'flush',但它仍然有效。 (Linux Mint,Python 2.7.5) -
@furas 在此处的 windows 7 中的 python 2.7 它没有...
-
@furas 哦,你真的评论了
print 'hey'行...顺便说一句,只需运行更新的代码 -
@K DawG 新版本(与旧版本一样)可以使用和不使用
print 'hey',但我使用 Linux。只能是 Windows 问题。
标签: python multithreading python-2.7 tkinter stdout