使用子进程PIPE输出更新Tkinter Canvas,需要线程吗?

时间:2017-01-25 18:20:25

标签: python multithreading canvas tkinter subprocess

对于我之前的问题的答案,我的estimatekeleton.py脚本现在与subprocess.Popen调用完美配合。 while True函数也可以工作,而在estimatekeleton.py脚本运行时,我得到了Console中显示的所有输出。现在我想在Canvas中向用户显示脚本的进度/输出。在下面给出的代码中,为了清晰起见,我剪切了Tkinter GUI的东西。但是,脚本为我提供了控制台中的estimatekeleton.py的进度,但它并没有更新Canvas,尽管两个命令在True循环中都是相同的。

所以我的问题是:我是否需要对子进程调用进行线程化以便在画布上绘制estimatekeleton.py脚本的输出/进度?

祝你好运

编辑1:我该如何解决这个问题?子进程是否在一个Thread中,而while true循环在另一个中?

class canvasEditor():

    def __init__(self):
        self.cwidth=500
        self.cheight=300
        self.lastY=0
    #Funktionen für die Datei Menübar
    def clearCanvas(self,color="#FFFFFF"):
        #Formatierung des MappingCanvas
        canvas.configure(bg=color)
        canvas.delete("all")
    def clearCanvas2(self):
        canvasSkelett.delete("all")
    def calcSkeleton():
        myCE.clearCanvas("#000000")  

def calcSkeleton():
    myCE.addCanvasContent("processing")
    myFileChooser.setPathToSkelett()
    os.chdir("/media/sf_Th_Neuro/Scripts/vCGPDM/code/tracking/")
    os.putenv("PYTHONPATH",os.pathsep.join([os.getenv("PYTHONPATH",""),"/media/sf_MyFolder/Scripts/code/"]))

    p = sub.Popen(["python", "estimateskeleton.py","--d",myFileChooser.estimationFile],
                      stdout=sub.PIPE,
                      stderr=sub.STDOUT)
    lastY=1
    while True:
        line = p.stdout.readline()
        if not line:
            break
        if lastY > 0:
            canvas.create_text(1,lastY,fill="green",text=line,anchor=NW)
            lastY += 12
        else:
            canvas.create_text(1,1,fill="green",text=line,anchor=NW)
            lastY += 12
        print line
        sys.stdout.flush()  

0 个答案:

没有答案