我想在python中将我的stdout打印到GUI。我正在使用Tkinter。 这是我为我工作在Python中逐行输出到GUI
def get_likes_button():
output = subprocess.Popen(['python', "getLikes.py"], stdout=subprocess.PIPE)
s1 = output.stdout.read()
text.delete("1.0",END)
text.insert(INSERT,s1)
但是,我得到了整个一次输出。 我想要的是GUI应该反复打印输出,就像它在终端上打印一样。
所以,我想这一点,但它给了我一个错误上点击按钮
def get_likes_button():
text.delete("1.0",END)
with subprocess.Popen(['python', "getLikes.py"], stdout=subprocess.PIPE) as output:
s1 = output.stdout.read()
text.insert(INSERT,s1)
的错误是
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "gui.py", line 88, in get_likes_button
with subprocess.Popen(['python', "getLikes.py"], stdout=subprocess.PIPE) as output:
AttributeError: __exit__
能否请你建议我该怎么办?
查看[这个问题](http://stackoverflow.com/q/14883648/4203871)了解如何使用线程将'stdout'重定向到窗口小部件的示例。 – chdorr