How to make a tkinter GUI work on top of a CUI program?

B

Beinan Li

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top