I know how to make a GUI program work on top of a console program like "ls", which exits immediately.
But some console programs have their own shell or ncurse-like CUI, such as cscope. So I figured that I need to first subprocess.popen a bidirectional pipe and send command through stdin and get results from stdout and stderr. But in such a case I found that communicate('cmd') will freeze. Am I in the right direction? Here is my code, in which the application connects to cscope through pipe, the GUI has only one button that tries to invoke a cscope search command and get the result, the Enter key is represented as \n in the stdin argument of communicate(): import sys, os, os.path, subprocess, shlex if sys.version_info.major < 3: import Tkinter as Tk else: import tkinter as Tk class MyGUI(object): ''' Frontend of the main command-line module. ''' def __init__(self): self.subProc = None self.root = Tk.Tk() self.frame = Tk.Frame(self.root) self.btn = Tk.Button(self.frame, text='My Command') self.btn.pack() self.btn.bind('<Button-1>', self.OnClickBtn) def MainLoop(self): os.chdir('path/to/myfolder') cmd = shlex.split('/usr/local/bin/cscope -d') self.subProc = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) self.root.mainloop() def OnClickBtn(self, event): print('OnClickBtn') (stdOut, stdErr) = self.subProc.communicate('symbolName\n') print(stdOut) if __name__ == '__main__': gui = MyGUI() gui.MainLoop()
-- https://mail.python.org/mailman/listinfo/python-list