0

I have the following script in Python :

import Tkinter
import subprocess
from Tkinter import *

top = Tkinter.Tk()
top.geometry( "800x600" )

def helloCallBack():
    #print "Below is the output from the shell script in terminal"
    text = Text( top )
    text.insert( INSERT, "*****************************************************\n" )
    text.insert( INSERT, "Below is the output from the shell script in terminal\n" )
    text.insert( INSERT, "*****************************************************\n" )
    p = subprocess.Popen( './secho.sh',                                      \
                          stdout = subprocess.PIPE,                          \
                          stderr = subprocess.PIPE,                          \
                          shell  = True                                      \
                          )
    output, errors = p.communicate()
    #--------------------------------------------------- DEBUG FRAMING STEP 1
    print "DEBUG [stdout] sent:", repr( output ), "<EoString>"
    print "DEBUG [stderr] sent:", repr( errors ), "<EoString>"
    #--------------------------------------------------- DEBUG FRAMING STEP 2
    text.insert( "end", "DEBUG-prefix-to-validate-Tkinter-action.<BoString>" \
                       + output                                              \
                       + "<EoString>"                                        \
                       )
    text.pack()
    #--------------------------------------------------- DEBUG FRAMING STEP 3
    print "DEBUG FRAMING <EoCall>"

B = Tkinter.Button( top, text ="Hello", command = helloCallBack )

B.pack()
top.mainloop()

When my shell script which is secho.sh has some simple commands such as ls, the program outputs normally as in the below screenshot. (I could not uplaod an image here as I am a newbee to stack overflow) http://tinyurl.com/tkinterout

Where as if I have some complex shell script, even if it is a one liner such as :

mail -f ~/Desktop/Test/Inbox.mbox

The display is just the text "Below is the output ...." and nothing else.

I have referred to this, this and many other stack overflow posts related to the same. But I could not get a satisfactory answer as none I found deals with commands such as mail ( which lets the user to interact with the terminal after executing the command).

How to tackle this problem?

Community
  • 1
  • 1
Technopolice
  • 467
  • 2
  • 5
  • 12
  • Kindly post what **`print output`**, **`print errors`** do contain in terminal and modify the code to show an indication upon being executed alike **`text.insert("end", "DEBUG-prefix-to-validate-action." + output + "" )`**. You may also have noticed, that the URL provided does not serve a .JPG image, so some repair may help in this direction. – user3666197 Oct 31 '14 at 09:38
  • @user3666197 I completely didnt get you. The script does not prints anything in the Tkinter window neither in the terminal other than the custom made "Below is the output from the shell script in terminal". Along side the the image URL which I have given here is working fine. I would appreciate if you can rephrase your sentence as I did not understand, and I can refine my post if you tell me in simple words :(. Sorry I am a newbee – Technopolice Oct 31 '14 at 09:46
  • if you do not mind, I would put the code into your OP to have the full context there. – user3666197 Oct 31 '14 at 10:17
  • @user3666197 Please go ahead. Feel free – Technopolice Oct 31 '14 at 10:21
  • When people have problems with a tkinter program, they should run it from a console window so there is a place to show error messages. – Terry Jan Reedy Oct 31 '14 at 20:47
  • This is really a 'getting output from a subprocess" problem. I would start with getting a normal script that successfully prints the output without using tkinter at all. If you can print it, you can insert it into a text widget. – Terry Jan Reedy Oct 31 '14 at 20:54
  • You inport tkinter twice, in different ways. Better to do `import tkinter as tk`, for instance, once. – Terry Jan Reedy Oct 31 '14 at 20:55
  • You said `mail -f ~/Desktop/Test/Inbox.mbox` waits for input. To get output, you have to make sure that the mail program closes. Either 1. add parameters to the invocation above so is does not wait, or add `stdin=subprocess.PIPE` to the subprocess call and add `input=xxx` to the communicate call, where `xxx` in the user input that causes mail to quit. – Terry Jan Reedy Oct 31 '14 at 21:08

1 Answers1

0

Here is a working example of what I suggested as the second alternative in the comment above.

import subprocess as sp

p = sp.Popen('''python -i -c "print('hello world')"''', stdin=sp.PIPE,
             stdout=sp.PIPE, stderr=sp.PIPE, universal_newlines=True)
out, err = p.communicate(input='quit()')
print("out = {}err = {}".format(out, err))

prints

out = hello world
err = >>> ... 

Without universal_newlines=True, the input arg must be bytes: p.communicate(input=b'quit()'). It appears that console interpreter prompts go to stderr, not stdout.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52