简体   繁体   中英

Tkinter Python script not displaying the output of shell script such as mail unix command

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM