我已經在python中創建了兩個模塊。其中一個模塊用於使用Tkinter創建GUI,第二個模塊用於捕獲和存儲圖像。當我在Tkinter模塊中調用opencv模塊時,它首先運行opencv模塊,釋放相機後運行Tkinter模塊。因此我使用子進程。 POPEN()。現在我想把輸入的子進程輸入到Tkinter模塊中。使用Tkinter創建GUI的代碼如下。如何在不終止子進程的情況下在調用進程中使用子進程的輸出?
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變量的值,並希望將它傳遞到入口而不終止子進程。
你不需要這裏的子進程。你可以使用'threading'或'multiprocessing'來運行'imacap'模塊中的代碼。 – jfs