subprocess.call() interacting with GUI (Tkinter) subprocess.call() interacting with GUI (Tkinter) tkinter tkinter

subprocess.call() interacting with GUI (Tkinter)


Think I've got the basics of using an Entry as stdin to subprocess. You may have to jiggle it about for your own needs (re: output to Text widget).

This example calls a test script:

# test.py:#!/usr/bin/env pythona = raw_input('Type something!: \n') #the '\n' flushes the promptprint a

that simply requires some input (from sys.stdin) and prints it.

Calling this and interacting with it via a GUI is done with:

from Tkinter import *import subprocessroot = Tk() e = Entry(root)e.grid()b = Button(root,text='QUIT',command=root.quit)b.grid()def entryreturn(event):    proc.stdin.write(e.get()+'\n') # the '\n' is important to flush stdin    e.delete(0,END)# when you press Return in Entry, use this as stdin # and remove ite.bind("<Return>", entryreturn)proc = subprocess.Popen('./test.py',stdin=subprocess.PIPE)root.mainloop()

Now whatever is typed into Entry e (followed by the Return key), is then passed via stdin to proc.

Hope this helps.


Also check this for ideas about stdout of subprocess question. You'll need to write a new stdout to redirect stdout to the textwidget, something like:

class MyStdout(object):    def __init__(self,textwidget):        self.textwidget = textwidget    def write(self,txt):        self.textwidget.insert(END,txt)sys.stdout = MyStdout(mytextwidget)

but I would recommend reading other examples where people have achieved this.