【问题标题】:Unexpected Text Widget behavior in loop循环中的意外文本小部件行为
【发布时间】: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


    【解决方案1】:

    问题是事件必须得到服务才能更新显示。这是 Tk(以及 Tkinter)工作方式的基础。 Python 中最简单的方法可能是将来自子进程的读取放在它自己的线程中,然后将消息中的每一行发送到主 GUI 线程进行显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多