Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'argument': 0.05; 'frontend': 0.07; 'result,': 0.07; 'tkinter': 0.07; 'tries': 0.07; '__name__': 0.09; 'exits': 0.09; 'sys,': 0.09; 'subject:How': 0.10; 'def': 0.12; 'gui': 0.12; "'''": 0.16; "'__main__':": 0.16; 'cmd': 0.16; 'command-line': 0.16; 'direction?': 0.16; 'event):': 0.16; 'self.root': 0.16; 'stdout': 0.16; 'subject:GUI': 0.16; 'subject:make': 0.16; 'subject:program': 0.16; 'subject:top': 0.16; '<': 0.19; 'connects': 0.19; 'command': 0.22; 'code,': 0.22; 'import': 0.22; 'shell': 0.22; 'message-id:@mail.gmail.com': 0.30; 'invoke': 0.31; 'os,': 0.31; 'pipe': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'module.': 0.36; 'subject:?': 0.36; 'application': 0.37; 'button': 0.38; 'skip:o 20': 0.38; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'first': 0.61; 'such': 0.63; 'here': 0.66; 'header:Reply-To:1': 0.67; 'results': 0.69; 'reply-to:no real name:2**0': 0.71; 'reply- to:addr:gmail.com': 0.80 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:reply-to:date:message-id:subject:from:to:content-type; bh=c0DyTtSBrrUxU50/S3SCXrbENHUxqK2qxx/xGPa9cYg=; b=AiYgEiQRxIs3QM3Tp8xC6gQef6CTnyP9nIQ0qwcrq1Rq9BOV3TJ7SMYXe15ac0MsZE Z1nn+tS/7IQCjqNMykOJ3R38WDbpUvclGz864sGtb5LttcMUXPXeknuBe5gSQL3JZJIW 38mjYD4nauOx14VSQYEHr5kP4EtR4vIOeVDKwNxcQkEYdtkyqvM8rLB5NJPbItcGj1p7 CVF+3r19nfEVZNTFjjfPjM9c7hphNVal+fTaroXFL2OGIrpW4/KlQ3Pqh4KEVFuZDdrC sGgxUwxY7cY94GihidIkNUWkjlm8Gg8Ld563GIx5T8EsCI/9b6Qq/qRAAdNxKBrjRGk5 ZoEQ== MIME-Version: 1.0 X-Received: by 10.58.90.1 with SMTP id bs1mr44912veb.29.1388803441749; Fri, 03 Jan 2014 18:44:01 -0800 (PST) Date: Fri, 3 Jan 2014 21:44:01 -0500 Subject: How to make a tkinter GUI work on top of a CUI program? From: Beinan Li To: python-list@python.org Content-Type: multipart/alternative; boundary=089e01184574cf030604ef1bff68 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: li.beinan@gmail.com List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 111 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1388803444 news.xs4all.nl 2890 [2001:888:2000:d::a6]:55015 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63098 --089e01184574cf030604ef1bff68 Content-Type: text/plain; charset=ISO-8859-1 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('', 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() --089e01184574cf030604ef1bff68 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
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.<= /div>
So I figured that I need to first subprocess.popen a bidirectional pip= e 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?

H= ere is my code, in which the application connects to cscope through pipe, t= he 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 o= f 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= =3D None
self.root =3D T= k.Tk()
self.frame =3D Tk.= Frame(self.root)
self.btn= =3D Tk.Button(self.frame, text=3D'My Command')
self.btn.pack()
self.btn.bind(&= #39;<Button-1>', self.OnClickBtn)
def MainLoop(self):=
os.chdir(= 9;path/to/myfolder')
cmd =3D shlex.split('/usr/local/bin/cscope -d')
self.subProc =3D s= ubprocess.Popen(cmd, shell=3DFalse, stdout=3Dsubprocess.PIPE, stderr=3Dsubp= rocess.PIPE, bufsize=3D0)
self.root.mainloop()
def OnClickBtn(self, event):
print('OnCli= ckBtn')
(stdOut, stdErr) = =3D self.subProc.communicate('symbolName\n')
print(stdOut)
if __name__ =3D=3D '__main__':
gui=A0=3D MyGUI()
gui.MainLoop()

<= /div>
--089e01184574cf030604ef1bff68--