如何在不终止子进程的情况下在被调用的进程中使用子进程的输出? [英] How to use output of childprocess in called process without terminating child process?

查看:118
本文介绍了如何在不终止子进程的情况下在被调用的进程中使用子进程的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在python中创建了两个模块.其中一个模块用于使用Tkinter创建GUI,第二个模块用于捕获和存储图像.当我在Tkinter模块中调用opencv模块时,它首先运行opencv模块,释放相机后,它正在运行Tkinter模块.因此,我使用了子进程. Popen().现在我想将子流程的输出输出到Tkinter模块中.使用Tkinter创建GUI的代码如下.

I have created two modules in python. One of the module is for creating GUI using Tkinter and second one is for capturing and storing images.When I called opencv module in Tkinter module,it runs opencv module first and after releasing camera,it was running Tkinter module.So I used subprocess.Popen(). Now I want output of subprocess into Tkinter module. The code for creating GUI using Tkinter is as follows.

import sys
from Tkinter import *
import Tkinter
import subprocess
def capcam():
    command="python2 imacap.py"
    subprocess.Popen(command,shell=True)
root=Tk()
add=Frame(root)
add.grid()
root.title("Test")
capcam()
button_height=11
button_width=29
button_ipadx=2
button_ipady=2
text_box = Entry(add,justify=RIGHT,width=100, font=100)
text_box.grid(row = 0, column = 1,columnspan = 5)
text_box.insert(0, "0")
bttn_3 = Button(add, height= button_height ,width= button_width,text = "3")
bttn_3.grid(row = 3, column = 2, padx=button_ipadx, pady=button_ipady)

以下是子进程的代码.

import cv2.cv as cv
capture = cv.CaptureFromCAM(0)
num = 0
while True:
    img = cv.QueryFrame(capture)
    cv.SaveImage('pic'+str(num)+'.jpg', img)
    if num == 500:
        del(capture)
        break
    if cv.WaitKey(10) == 27:
        break
    num += 1

我希望在mainprocess的运行时提供num变量的值,并希望将其传递给条目而不终止子进程.

I want the value of num variable at runtime in mainprocess and want to pass it to entry without terminating child process.

推荐答案

完整的代码示例: tkinter-read-async-subprocess-output.py 演示了如何在不使用线程的情况下读取子流程输出Tkinter.它在GUI中显示输出,并在按下按钮时停止子进程.

The complete code example: tkinter-read-async-subprocess-output.py demonstrates how to read the subprocess output without threads using Tkinter. It shows the output in the GUI and stops the subprocess on a button press.

如果tk.createfilehandler()在您的系统上不起作用;您可以尝试使用后台线程代替.有关代码示例,请参见 kill-process.py .

If tk.createfilehandler() doesn't work on your system; you could try to use a background thread instead. See kill-process.py for the code example.

这篇关于如何在不终止子进程的情况下在被调用的进程中使用子进程的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆