【发布时间】:2011-11-16 09:26:06
【问题描述】:
请参考以下代码: 我能够实时(逐行)打印两个打印语句(在#1 和#2)。然而,文本小部件仅在子进程 csh 脚本执行后出现。
当我能够打印从子进程的标准输出接收到的值时,为什么它不显示在文本小部件上?
#!/tools//python/2.7.2/bin/python -u
import os
import ttk
from Tkinter import *
import subprocess
import sys
t = Tk()
t.title('New title')
t.geometry('800x1000-5+40')
t.state('normal')
little = Label(t, text="OUTPUT LOG").grid(column = 0, row = 0)
log = Text(t, state='normal', width=115, height=150, wrap='none')
log.grid(row = 1, column = 0)
test=subprocess.Popen("tem",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
#tem is a csh file which print number from 1 to 1000
#stdout
cnt = 0.0
while True:
line = test.stdout.readline()
cnt += 1
if line == "":
break
else:
log['state'] = 'normal'
log.insert(cnt,line)
print 'line #' #1
print line #2
log['state'] = 'disabled'
t.mainloop()
【问题讨论】:
标签: python widget tkinter tk subprocess