1

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)

Following is the code of child process.

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

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

user3352710
  • 79
  • 1
  • 3
  • 9
  • you don't need a subprocess here. You could use `threading` or `multiprocessing` to run the code from `imacap` module. – jfs Mar 01 '14 at 19:10

2 Answers2

2

Hold a reference

p = subprocess.Popen(command,shell=True, stdout = subprocess.PIPE)

Then you can do

num = int(p.stdout.readline()) # blocks!

if you do

print(num) in the child process

Also have a look at the module multiprocessing. It can solve the problem in other ways.

User
  • 14,131
  • 2
  • 40
  • 59
  • +1 for `stdout=PIPE`. To avoid blocking the GUI on `p.stdout.readline()`, you could [use `tk.createfilehandler()` or read in a background thread](http://stackoverflow.com/a/22118914/4279) – jfs Mar 01 '14 at 19:09
1

Can I have Tk events handled while waiting for I/O? shows how to avoid blocking while reading a subprocess output from GUI thread. Assuming imacap.py has print(num) in it:

def read_output(self, pipe, mask):
    data = os.read(pipe.fileno(), 1 << 20)
    if not data: # eof
        root.deletefilehandler(proc.stdout)
    else:
        print("got: %r" % data)

proc = Popen(["python2", "imacap.py"], stdout=PIPE, stderr=STDOUT)
root.createfilehandler(proc.stdout, READABLE, read_output)

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.

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.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • I did not even know that something like this existed. Totally cool. I guess you could even use sockets with that! – User Mar 01 '14 at 20:59
  • @User: it is a common feature for GUI frameworks e.g., `gtk` has [`io_add_watch()`](http://stackoverflow.com/a/15376317/4279), `qt` allows to specify [callbacks for QProcess](http://stackoverflow.com/a/22110924/4279). `twisted`'s [`reactor.spawnProcess()`](http://stackoverflow.com/a/5750194) and `asyncio`'s (Python 3.4+) [`loop.subprocess_exec()`](http://stackoverflow.com/a/20697159/4279) also allow to read subprocess output asynchroniously in a portable manner. – jfs Mar 01 '14 at 21:52
  • And yes, sockets should be even more portable, you can even [mix sockets and subprocesses on some systems](https://gist.github.com/zed/7454768). – jfs Mar 01 '14 at 21:57